Home >Web Front-end >JS Tutorial >How to Disable and Enable HTML Input Fields with jQuery?

How to Disable and Enable HTML Input Fields with jQuery?

Patricia Arquette
Patricia ArquetteOriginal
2024-12-11 20:28:11959browse

How to Disable and Enable HTML Input Fields with jQuery?

How to Disable or Enable an Input with jQuery

When working with HTML input fields, it is often necessary to disable or enable them for various reasons. jQuery provides multiple ways to accomplish these tasks.

Standard Method (jQuery 1.6 )

Since jQuery 1.6, the preferred method to modify the disabled property is through the .prop() function.

Disable:

$("input").prop('disabled', true);

Enable:

$("input").prop('disabled', false);

jQuery 1.5 and Below

In jQuery versions prior to 1.6, the .prop() function is not available. Instead, use the .attr() function:

Disable:

$("input").attr('disabled','disabled');

Enable:

$("input").removeAttr('disabled');

DOM Object Method

Regardless of the jQuery version, you can directly access the DOM object and modify its disabled property:

Disable:

this.disabled = true;

Enable:

this.disabled = false;

Note for jQuery 1.6 :

In jQuery versions 1.6 , a .removeProp() method is available. However, it should not be used to remove native properties like disabled, as it permanently removes them. Instead, use .prop() to set the property to false.

The above is the detailed content of How to Disable and Enable HTML Input Fields with jQuery?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn