Removing html5 required attribute with jQuery

Hi I would like to remove the 'required=""' attribute with jquery.

<input 
   type="text" 
   id="edit-submitted-first-name" 
   name="submitted[first_name]" 
   value="" 
   size="30" 
   maxlength="128" 
   required=""
>

Solution 1:

Just:

$('#edit-submitted-first-name').removeAttr('required');​​​​​

If you're interested in further reading take a look here.

Solution 2:

If you want to set required to true

$(document).ready(function(){
$('#edit-submitted-first-name').prop('required',true);
});

if you want to set required to false

$(document).ready(function(){
$('#edit-submitted-first-name').prop('required',false);
});

Solution 3:

Using Javascript:

document.querySelector('#edit-submitted-first-name').required = false;
  • More Info Here

Using jQuery:

$('#edit-submitted-first-name').removeAttr('required');
  • More Info Here

Solution 4:

$('#id').removeAttr('required');​​​​​
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

Solution 5:

Even though the ID selector is the simplest, you can also use the name selector as below:

$('[name='submitted[first_name]']').removeAttr('required');

For more see: https://api.jquery.com/attribute-equals-selector/