Home >Web Front-end >JS Tutorial >How Do I Disable and Enable Input Fields Using jQuery?
How to Disable or Enable an Input with jQuery
Introduction:
Input fields can be crucial for user interaction, and sometimes, disabling or enabling them is essential for controlling user actions. jQuery provides robust methods for these operations.
Disable an Input:
In jQuery versions 1.6 and above, use the .prop() function to set the disabled property:
$("input").prop('disabled', true);
For jQuery 1.5 and below, use the .attr() function to set the disabled attribute:
$("input").attr('disabled','disabled');
Alternatively, you can directly modify the DOM object's disabled property:
// within an event handler this.disabled = true;
Enable an Input:
Again, for jQuery 1.6 , use .prop() to set the disabled property to false:
$("input").prop('disabled', false);
In jQuery 1.5 and below, use .removeAttr() to remove the disabled attribute:
$("input").removeAttr('disabled');
As with disabling, you can also directly set the DOM object's disabled property to false.
Note for jQuery 1.6 :
The .removeProp() function should not be used on native properties like 'disabled'. Instead, set the properties to false using .prop().
Conclusion:
Disabling or enabling inputs with jQuery is straightforward, and you can choose the method that best suits your specific version of jQuery. Remember to consider performance and browser compatibility when making your selection.
The above is the detailed content of How Do I Disable and Enable Input Fields Using jQuery?. For more information, please follow other related articles on the PHP Chinese website!