.attr() | jquery get attribute
Description:Getthevalueofanattributeforthefirstelementinthesetofmatchedelements.The.attr()methodgetstheattributevalueforonlythefirstelementinthematchedset.Togetthevalueforeachelementindividually,usealoopingconstructsuchasjQuerys.each()or.map()method.UsingjQuerys.attr()methodtogetthevalueofanelementsattributehastwomainbenefits:Convenience:ItcanbecalleddirectlyonajQueryobjectandchainedtootherjQuerymethods.Cross-browserconsistency:Thevaluesofsomeattributesarereportedinconsistentlyacrossbrowsers...
Description: Get the value of an attribute for the first element in the set of matched elements.
The .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 jQuerys .each() or .map() method.
Using jQuerys .attr() method to get the value of an elements 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.Note: Attribute values are strings with the exception of a few attributes such as value and tabindex.
As of jQuery 1.6, the .attr() method returns undefined for attributes that have not been set. To retrieve and change DOM pr...