JavaScript getAttribute() Method Tutorial

In this section, we will learn what the Element getAttribute() method is and how to use it in JavaScript.

What is Element getAttribute() Method in JavaScript?

The JavaScript Element getAttribute() method is used to get the value of an attribute in an HTML element.

Note: using this method, we get the string value of the specified attribute. But there’s another method called getAttributeNode() that will return an AttributeNode object that has a bunch of methods and properties by which you can get the value of the specified attribute and also can run other operations that you’ll learn more about them in its section.

Element getAttribute() Method Syntax:


Element getAttribute() Method Parameter

The method takes one argument and that is the attribute we want to get its associated value.

Element getAttribute() Method Return Value

The return value of this method is of type string and is equal to the value of the specified argument.

Note: if the specified attribute wasn’t declared in the target element, the return value could be either a null or an empty string.

Example: get attribute value in JavaScript

See the Pen get attribute value in JavaScript by Omid Dehghan (@odchan1) on CodePen.


Top Technologies