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

5 Answers 5

156

Just:

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

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

Sign up to request clarification or add additional context in comments.

Comments

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

2 Comments

The best possible way to do this. I have tried other solution but this one helps me a lot. thx
Thanks. I was trying the "removeProp('required')" function but that didn't do anything. This worked great.
34

Using Javascript:

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

Using jQuery:

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

1 Comment

jQuery handles such checks for you so you don't have to explicitly check the attribute exists before attempting to remove it.
15

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

Comments

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/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.