Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

element
jquery
communitycreator

What is elem.attr() in JQuery?

Theodore Kelechukwu Onyejiaku

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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/edpresso">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.

RELATED TAGS

element
jquery
communitycreator

CONTRIBUTOR

Theodore Kelechukwu Onyejiaku

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring