[jquery] 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=""
>

This question is related to jquery attributes

The answer is


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


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);
});

Using Javascript:

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

Using jQuery:

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

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/


Just:

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

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