How to Disable enable an input with jQuery
- To change the disabled property we can use the .prop() function.
- The .prop() function doesn’t exist, but .attr() does similar:
- To Set the disabled attribute.
- To enable again, the proper method is to use .removeAttr()
In any version of jQuery
- We can always rely on the actual DOM object and is probably a little faster than the other two options if we are only dealing with one element:
- The advantage of using the .prop() or .attr() methods is that we can set the property for a bunch of selected items.
- Do not use this method to remove native properties such as checked, disabled, or selected. This will remove the property completely and, once removed, cannot be added again to element. Use .prop() to set these properties to false instead.
- We can enable or disable a form element using the .prop() method:
- We can place this somewhere global in your code:
And then we can write things like:
- If we want to invert the current state (like a toggle button behavior):
In Jquery mobile:
- Here is the solution for Disable or Enable Form Element in jQuery: