Possible Duplicate:
.prop() vs .attr()
Is there a difference between an attribute
and a property
in the jQuery
terminology? Is there an example that can clarify this point?
As of jQuery 1.6, the . prop() method provides a way to explicitly retrieve property values, while . attr() retrieves attributes. For example, selectedIndex, tagName, nodeName, nodeType, ownerDocument, defaultChecked, and defaultSelected should be retrieved and set with the .
The attributes have a data type of string. So no matter the value of the attribute, it will always return a string. Property: In contrast to the attributes, which are defined in HTML, properties belong to the DOM. Since DOM is an object in JavaScript, we can get and set properties.
jQuery deprecated (version 1.6) and removed (version 1.9) the use of . attr() for use to set "checked" and "disabled" properties. It also points out that attempting to retrieve values by using . attr() will result in confusion/problems, as in this example: $elem.
jQuery attr() Method The attr() method sets or returns attributes and values of the selected elements. When this method is used to return the attribute value, it returns the value of the FIRST matched element.
Is there a difference between an attribute and a property in the jQuery terminology?
Yes, there is a difference.
For example...
<input type="text" name="age" value="25" />
jsFiddle.
The attribute would be the value
attribute as in the markup. You would use attr('value')
.
The property would be the value
property as accessed via the DOM API. You would use prop('value')
(strictly speaking, you'd use val()
).
Sometimes they can be very similar, but not always.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With