Removing html5 required attribute with jQuery

JqueryAttributes

Jquery Problem Overview


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=""
>

Jquery Solutions


Solution 1 - Jquery

Just:

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

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

Solution 2 - Jquery

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 - Jquery

Using Javascript:

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

Using jQuery:

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

Solution 4 - Jquery

$('#id').removeAttr('required');​​​​​

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

Solution 5 - Jquery

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/

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLeBlaireauView Question on Stackoverflow
Solution 1 - JqueryMinko GechevView Answer on Stackoverflow
Solution 2 - JqueryLatief AnwarView Answer on Stackoverflow
Solution 3 - JquerypalaѕнView Answer on Stackoverflow
Solution 4 - JqueryNijin P JView Answer on Stackoverflow
Solution 5 - JqueryArisView Answer on Stackoverflow