Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery: Checking for disabled attribute and adding/removing it?

Tags:

I select all input elements of a form like this:

var fields = $( form + " :input" ).not( "button" ); 

How do I check if any of these inputs has the disabled attribute set and remove it when present?

Also, I need to add it after is has been removed (and serialize the fields in between), is there an elegant way for this? Something like toggle but for attributes?

like image 520
Sven Avatar asked Feb 16 '13 18:02

Sven


People also ask

How do I remove disabled attributes?

To remove the disabled attribute, select the element and call the removeAttribute() method on it, passing it disabled as a parameter, e.g. btn. removeAttribute('disabled') . The removeAttribute method will remove the disabled attribute from the element.

How do I set disabled property in jQuery?

Projects In JavaScript & JQuery We can easily disable input box(textbox,textarea) using disable attribute to “disabled”. $('elementname'). attr('disabled','disabled'); To enable disabled element we need to remove “disabled” attribute from this element.

What jQuery method is used to completely remove attributes from elements?

To remove all attributes of elements, we use removeAttributeNode() method.


2 Answers

Assuming you are using jQuery 1.6 or above, the suggested method is to use .prop().

fields.prop("disabled", false);

If you need to have logic around each one, you could do something to the extent of

var fields = $( form + " :input" ).not( "button" ); fields.each(function(index, element) {     var isDisabled = $(element).is(':disabled');     if (isDisabled) {         $(element).prop('disabled', false);     } else {         // Handle input is not disabled     } }); 

jsfiddle

like image 82
Ruben Infante Avatar answered Oct 06 '22 00:10

Ruben Infante


You can use the disabled selector

$('input:disabled').attr('disabled', ''); 

See here

http://jsfiddle.net/JngR9/

like image 41
glosrob Avatar answered Oct 06 '22 02:10

glosrob