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

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

DDD
Release: 2024-10-19 19:03:29
Original
998 people have browsed it

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.
});
Copy after login

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!

source:php
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template