Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery - attribute vs. property [duplicate]

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?

like image 572
Simplicity Avatar asked Oct 03 '11 06:10

Simplicity


People also ask

What is difference between attribute and property in jQuery?

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 .

What is difference between attribute and property?

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.

Is jQuery attr deprecated?

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.

What is ATTR jQuery?

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.


1 Answers

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.

like image 96
alex Avatar answered Oct 28 '22 19:10

alex