Home  >  Article  >  Web Front-end  >  Why Use Prop() Instead of RemoveAttr() to Disable Form Inputs?

Why Use Prop() Instead of RemoveAttr() to Disable Form Inputs?

DDD
DDDOriginal
2024-10-19 19:03:29903browse

Why Use Prop() Instead of RemoveAttr() to Disable Form Inputs?

Removing the "disabled" Attribute with jQuery

In the given code, an attempt is made to disable inputs and then enable them again upon clicking a link. However, the solution doesn't function as expected.

The Solution:

To effectively remove the "disabled" attribute, use the prop() method instead of removeAttr(). The correct code would be:

$("#edit").click(function(event){
   event.preventDefault();
   $('.inputDisabled').prop("disabled", false); // Element(s) are now enabled.
});

Why Use prop()?

prop() should be used for properties like "disabled," while attr() should be used for attributes. In pre-jQuery 3.0 versions, removeAttr() would also set the corresponding boolean property to false, leading to incorrect behavior in modern browsers. prop(), on the other hand, only sets the property value without affecting the attribute.

Additional Note:

jsFiddle Example: https://jsfiddle.net/

The above is the detailed content of Why Use Prop() Instead of RemoveAttr() to Disable Form Inputs?. 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