95

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
  • 58,414
  • 16
  • 114
  • 157
LeBlaireau
  • 17,133
  • 33
  • 112
  • 192

5 Answers5

154

Just:

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

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

j0k
  • 22,600
  • 28
  • 79
  • 90
Minko Gechev
  • 25,304
  • 9
  • 61
  • 68
65

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,833
  • 17
  • 27
34

Using Javascript:

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

Using jQuery:

$('#edit-submitted-first-name').removeAttr('required');
palaѕн
  • 72,112
  • 17
  • 116
  • 136
  • 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
15

$('#id').removeAttr('required');​​​​​
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
Nijin P J
  • 1,302
  • 1
  • 9
  • 15
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,643
  • 1
  • 41
  • 38