Get Element By Attribute Jquery

Get the value of an attribute for the first element in the set of matched elements or phối one or more attributes for every matched element.

Bạn đang xem: Get element by attribute jquery


Contents: .attr( attributeName ).attr( attributeName ) .attr( attributeName, value ) .attr( attributeName, value ) .attr( attributes ) .attr( attributeName, function )

Description: Get the value of an attribute for the first element in the set of matched elements.

version added: 1.0 .attr( attributeName )

The .attr() method gets the attribute value for only the first element in the matched mix. To get the value for each element individually, use a looping construct such as obatambeienwasirherbal.com's .each() or .map() method.

Using obatambeienwasirherbal.com's .attr() method lớn get the value of an element's attribute has two main benefits:

Convenience: It can be called directly on a obatambeienwasirherbal.com object và chained khổng lồ other obatambeienwasirherbal.com 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.

Note: Attribute values are strings with the exception of a few attributes such as value và tabindex.


As of obatambeienwasirherbal.com 1.6, the .attr() method returns undefined for attributes that have not been set. To retrieve sầu & change DOM properties such as the checked, selected, or disabled state of khung elements, use the .prop() method.

Xem thêm: Top 5 Phần Mềm Klick - Top 5 Phần Mềm Auto Click Chuột Tốt Nhất Hiện Nay

Attributes vs. Properties

The difference between attributes and properties can be important in specific situations. Before obatambeienwasirherbal.com 1.6, the .attr() method sometimes took property values into lớn account when retrieving some attributes, which could cause inconsistent behavior. As of obatambeienwasirherbal.com 1.6, the .prop() method provides a way lớn explicitly retrieve sầu property values, while .attr() retrieves attributes.

For example, selectedIndex, tagName, nodeName, nodeType, ownerDocument, defaultChecked, và defaultSelected should be retrieved và set with the .prop() method. Prior lớn obatambeienwasirherbal.com 1.6, these properties were retrievable with the .attr() method, but this was not within the scope of attr. These vày not have sầu corresponding attributes and are only properties.

Concerning boolean attributes, consider a DOM element defined by the HTML markup , và 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" ) (pre-1.6) true (Boolean) Changed with checkbox state

According khổng lồ 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 phối lớn empty string value or even "false". This is true of all boolean attributes.

Nevertheless, the most important concept khổng lồ rethành viên about the checked attribute is that it does not correspond to lớn the checked property. The attribute actually corresponds khổng lồ the defaultChecked property & should be used only to mix 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 và value.


In Internet Explorer prior to lớn version 9, using .prop() to lớn phối a DOM element property to anything other than a simple primitive sầu 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 mix values on DOM objects without memory leaks, use .data().