I have a html field.
<select name="select-states" disabled="disabled"><option value="">Choose State »</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
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.
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.
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.
The :disabled selector selects all disabled form elements.
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');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With