Removing html5 required attribute with jQuery
Asked Answered
C

5

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=""
>
Curriery answered 19/12, 2012 at 11:24 Comment(0)
R
154

Just:

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

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

Romona answered 19/12, 2012 at 11:26 Comment(0)
A
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);
});
Amiens answered 14/8, 2017 at 9:2 Comment(2)
The best possible way to do this. I have tried other solution but this one helps me a lot. thxProductive
Thanks. I was trying the "removeProp('required')" function but that didn't do anything. This worked great.Turtleback
G
34

Using Javascript:

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

Using jQuery:

$('#edit-submitted-first-name').removeAttr('required');
Grovel answered 19/12, 2012 at 11:33 Comment(1)
jQuery handles such checks for you so you don't have to explicitly check the attribute exists before attempting to remove it.Seroka
D
15

$('#id').removeAttr('required');​​​​​
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
Dowlen answered 13/8, 2019 at 13:39 Comment(0)
K
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/

Karlin answered 15/5, 2019 at 8:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.