From the original OS question Disable / enable an input with jQuery?
I want to enable and disable elements of an HTML page (be it inputs, divs, buttons, etc.).
How can I solve it using jQuery?
From the response of the original OS Disable / enable an input with jQuery?
To change the
disabled property you must use the
$("input").prop('disabled', true); $("input").prop('disabled', false);
.prop() function does not exist, but
.attr() works similarly:
Set the disabled attribute:
To enable again, the appropriate method is to use
You can always trust the current DOM object and it's probably a little faster than the other two options if it's just an element:
// assuming an event handler thus 'this' this.disabled = true;
The advantage of using the
.prop () or
.attr () methods is that you can set the property for a bunch of selected items.
Note: In 1.6 there is a method
.removeProp() that sounds a lot like
removeAttr() , but < strong> MUST NOT BE USED in native properties as
'disabled' Excerpt from documentation:
Note: Do not use this method to remove native properties such as checked, disabled, or selected. This will remove the property completely and, once removed, can not be added again to element. Use .prop () to set these properties to false instead.
Note: Do not use this method to remove native properties, such as checked, disabled or selected. This will completely eliminate the property and, once deleted, can not be added back to the element. Use .prop () to set these properties to false in your place.
In fact, I doubt there are many legitimate uses for this method, Boolean properties are made in such a way that you must set them to false instead of "delete" them as their "attribute" equivalents in 1.5