Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i add disabled=disabled attribute to <select> using jQuery?

Tags:

jquery

select

I have a html field.

<select name="select-states" disabled="disabled"><option value="">Choose State &raquo;</option></select>

initially the field is disabled, i want to enable or disable it based on events. to enable it i am using the following jQuery which works fine.

$('select[name="select-states"]').removeAttr('disabled');

I want to re-enable it using jQuery, for which i tried using this which does not work.

$('select[name="select-states"]').attr('disabled');

What am i missing? which is the correct syntax for this?

thank you

like image 815
Ibrahim Azhar Armar Avatar asked Jul 28 '11 12:07

Ibrahim Azhar Armar


People also ask

How do I set disabled property in jQuery?

The disable/enable an input element in jQuery can be done by using prop() method. The prop() method is used to set or return properties and values for the selected elements.

How do I add a disabled attribute?

To set the disabled attribute, select the element and call the setAttribute() method on it, passing it disabled as the first parameter, e.g. button. setAttribute('disabled', '') . The setAttribute method will add the disabled attribute to the element.

How add and remove disabled property in jQuery?

To remove disabled attribute using jQuery, use the removeAttr() method. You need to first remove the property using the prop() method. It will set the underlying Boolean value to false.

What does the disabled select in jQuery?

The :disabled selector selects all disabled form elements.


1 Answers

In your code, you provide only one parameter to .attr(). This indicates that the function should just return the value for the disabled attribute of that element.

To set the disabled attribute, provide the attribute name (first parameter) along with a value (second parameter):

$('select[name="select-states"]').attr('disabled', 'disabled');
like image 54
Jon Gauthier Avatar answered Nov 15 '22 01:11

Jon Gauthier