What is elem.attr() in JQuery?

Overview

elem.attr() is a JQuery method that is used to get the value of an element’s attribute. It only gets the attribute value of the first match.


Attributes are used to provide additional information about an element in HTML.


See the href attribute of the anchor <a> element below:


<a href="https://educative.io/answers">VIsit Edpresso!</a>

Syntax


elem.attr(attribute)

Parameter

  • attribute: This is the attribute that is present in an element and that we want to know the value of.

Return value

If the element has the attribute, this method returns the value of the attribute passed as a parameter.

Code

In the example below, we create some HTMLHyperText Markup Language elements and use the .attr() method to get some of their attribute values.

Click the button to alert the attributes of elements on the webpage below.

Free Resources