.prop( propertyName )Returns: Anything
Description: Get the value of a property for the first element in the set of matched elements.
-
version added: 1.6.prop( propertyName )
-
propertyNameType: StringThe name of the property to get.
-
The .prop() method gets the property value for only the first element in the matched set. It returns undefined for the value of a property that has not been set, or if the matched set has no elements. To get the value for each element individually, use a looping construct such as jQuery's .each() or .map() method.
Note: Attempting to change the type property (or attribute) of an input element created via HTML or already in an HTML document will result in an error being thrown by Internet Explorer 6, 7, or 8.
Attributes vs. Properties
The difference between attributes and properties can be important in specific situations. Before jQuery 1.6, the .attr() method sometimes took property values into account when retrieving some attributes, which could cause inconsistent behavior. 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 .prop() method. Prior to jQuery 1.6, these properties were retrievable with the .attr() method, but this was not within the scope of attr. These do not have corresponding attributes and are only properties.
Concerning boolean attributes, consider a DOM element defined by the HTML markup <input type="checkbox" checked="checked" />, and assume it is in a JavaScript variable named elem:
elem.checked
|
true (Boolean) Will change with checkbox state |
|---|---|
$( elem ).prop( "checked" )
|
true (Boolean) Will change with checkbox state |
elem.getAttribute( "checked" )
|
"checked" (String) Initial state of the checkbox; does not change |
$( elem ).attr( "checked" )
(1.6)
|
"checked" (String) Initial state of the checkbox; does not change |
$( elem ).attr( "checked" )
(1.6.1+)
|
"checked" (String) Will change with checkbox state |
$( elem ).attr( "checked" )
(pre-1.6)
|
true (Boolean) Changed with checkbox state |
According to the W3C forms specification, the checked attribute is a boolean attribute, which means the corresponding property is true if the attribute is present at all—even if, for example, the attribute has no value or is set to empty string value or even "false". This is true of all boolean attributes.
Nevertheless, the most important concept to remember about the checked attribute is that it does not correspond to the checked property. The attribute actually corresponds to the defaultChecked property and should be used only to set the initial value of the checkbox. The checked attribute value does not change with the state of the checkbox, while the checked property does. Therefore, the cross-browser-compatible way to determine if a checkbox is checked is to use the property:
-
if ( elem.checked ) -
if ( $( elem ).prop( "checked" ) ) -
if ( $( elem ).is( ":checked" ) )
The same is true for other dynamic attributes, such as selected and value.
Additional Notes:
-
In Internet Explorer prior to version 9, using
.prop()to set a DOM element property to anything other than a simple primitive value (number, string, or boolean) can cause memory leaks if the property is not removed (using.removeProp()) before the DOM element is removed from the document. To safely set values on DOM objects without memory leaks, use.data().
Example:
Display the checked property and attribute of a checkbox as it changes.
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
|