Contents:
.attr( attributeName )Returns: String
Description: Get the value of an attribute for the first element in the set of matched elements.
-
version added: 1.0.attr( attributeName )
-
attributeNameType: StringThe name of the attribute to get.
-
.attr()
method gets the attribute value for only the first element in the matched set. To get the value for each element individually, use a looping construct such as jQuery's .each()
or .map()
method.
.attr()
method to get the value of an element's attribute has two main benefits:
- Convenience: It can be called directly on a jQuery object and chained to other jQuery methods.
-
Cross-browser consistency: The values of some attributes are reported inconsistently across browsers, and even across versions of a single browser. The
.attr()
method reduces such inconsistencies.
.attr()
method returns undefined
for attributes that have not been set. To retrieve and change DOM properties such as the checked
, selected
, or disabled
state of form elements, use the .prop() method.
Attributes vs. Properties
.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.
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.
<input type="checkbox" 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" )
|
"" (String) Initial state of the checkbox; does not change |
$( elem ).attr( "checked" )
(4.0+)
|
"" (String) Initial state of the checkbox; does not change |
$( elem ).attr( "checked" )
(1.6-3.x)
|
"checked" (String) Initial state of the checkbox; does not change |
$( elem ).attr( "checked" )
(pre-1.6)
|
true (Boolean) Changed with checkbox state |
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.
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" ) )
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()
.
Examples:
Example 1
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
34
|
|
Demo:
Example 2
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
|
|
Demo:
.attr( attributeName, value )Returns: jQuery
Description: Set one or more attributes for the set of matched elements.
-
version added: 1.0.attr( attributeName, value )
-
attributeNameType: StringThe name of the attribute to set.
-
valueA value to set for the attribute. If
null
, the specified attribute will be removed (as in.removeAttr()
). Non-ARIA attributes can also be removed by passingfalse
.
-
-
version added: 1.0.attr( attributes )
-
attributesType: PlainObjectAn object of attribute-value pairs to set.
-
-
version added: 1.1.attr( attributeName, function )
-
attributeNameType: StringThe name of the attribute to set.
-
functionA function returning the value to set.
this
is the current element. Receives the index position of the element in the set and the old attribute value as arguments.
-
.attr()
method is a convenient way to set the value of attributes—especially when setting multiple attributes or using values returned by a function. Consider the following image:
1
|
|
Setting a simple attribute
alt
attribute, simply pass the name of the attribute and its new value to the .attr()
method:
1
|
|
1
|
|
Setting several attributes at once
alt
attribute and add the title
attribute at the same time, pass both sets of names and values into the method at once using a plain JavaScript object. Each key-value pair in the object adds or modifies an attribute:
1
2
3
4
|
|
Removing an attribute
.attr( name, null )
or use .removeAttr( name )
. For non-ARIA attributes, in jQuery 4.0+ you can also call .attr( name, false )
.
false
as the value for an attribute whose name starts with "aria-…"
will stringify that value to "false"
rather than remove the attribute. To guarantee removal of an attribute, use the .removeAttr()
method or provide null
as the value to the .attr()
setter.
type
attribute on an input
or button
element created via document.createElement()
will throw an exception on Internet Explorer 8 or older.
Computed attribute values
1
2
3
|
|
function(index, attr){}
), or if undefined
is returned, the current value is not changed. This is useful for selectively setting values only when certain criteria are met.
Examples:
Example 1
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
34
35
|
|
Demo:
Example 2
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
34
35
36
|
|
Demo:
Example 3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
|