85

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=""
>
isherwood
  • 52,576
  • 15
  • 105
  • 143
LeBlaireau
  • 16,087
  • 30
  • 105
  • 176

5 Answers5

145

Just:

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

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

j0k
  • 22,303
  • 28
  • 77
  • 86
Minko Gechev
  • 24,430
  • 7
  • 59
  • 67
52

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);
});
Latief Anwar
  • 1,533
  • 14
  • 26
31

Using Javascript:

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

Using jQuery:

$('#edit-submitted-first-name').removeAttr('required');
palaѕн
  • 68,816
  • 17
  • 108
  • 129
  • 7
    jQuery handles such checks for you so you don't have to explicitly check the attribute exists before attempting to remove it. – Anthony Grist Dec 19 '12 at 11:38
14

$('#id').removeAttr('required');​​​​​
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
Nijin P J
  • 1,177
  • 1
  • 7
  • 14
2

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/

Aris
  • 4,219
  • 1
  • 35
  • 36