1

I would like to remove any HTML 5 validation on all input elements. Preferably using pure javascript.

Thinking it would be nice to create a file that can be included when developing, which would bring certain functionallity. Such as removing all required attributes, dumping post data, error reporting on/off, etc.

I have some jquery to remove the attribute from certain elements:

$('div').removeAttr('required');​​​​​

But would like to see something that works without jquery and applys to all elements that support html5 validation.

atoms
  • 2,993
  • 2
  • 22
  • 43
  • possible duplicate of [How to remove an attribute from a DOM element using Javascript?](http://stackoverflow.com/questions/18770925/how-to-remove-an-attribute-from-a-dom-element-using-javascript) – Luizgrs Apr 09 '15 at 13:44
  • 2
    Have you tried using `novalidate` attribute on your form? – Tadeáš Peták Apr 09 '15 at 13:44
  • [Disable validation of HTML5 form elements](http://stackoverflow.com/a/3094185/1064325) – falsarella Apr 09 '15 at 13:47
  • @TadeášPeták thanks that would work great, however I envisige building a toolbar that allows me to disable/enable this withouth having to alter the HTML. – atoms Apr 09 '15 at 13:50

2 Answers2

1

Here is a JavaScript function that removes the specified attribute of all existing elements:

function removeAttribute(attribute) {

    var allDocuments = document.getElementsByTagName("*");

    for (var i=0; i < allDocuments.length; i++) {
        allDocuments[i].removeAttribute(attribute);
    }

}

To use this, simply call:

removeAttribute('required')
Joshua Arvin Lat
  • 1,029
  • 9
  • 8
1

This should do the trick:

document.querySelectorAll('[required]').forEach(function(value,index){
  value.removeAttribute('required');
})
Maciej Paprocki
  • 1,230
  • 20
  • 29