English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

HTML DOM getAttribute() method

HTML DOM Element Object

getAttribute()The method returns the value of the specified attribute on the element.

If the given attribute does not exist, the returned value will benullor "" (empty string).

If you want to use the attribute asAttr objectReturn, please usegetAttributeNode()Method.

Syntax:

element.getAttribute(attr)
var x = document.getElementsByTagName("H1")[0].getAttribute("class");
Test and See‹/›

Browser compatibility

All browsers fully support the getAttribute() method:

Method
getAttribute()IsIsIsIsIs

Parameter value

ParameterDescription
attrThe name of the attribute from which you want to retrieve the value

Technical details

Return value:A string that represents the value of the specified attribute
DOM Version:DOM Level1

More Examples

Get the value of the style attribute of the element:

var x = document.getElementsByTagName("H1")[0];
x.getAttribute("style");
Test and See‹/›

Get the href attribute value of the anchor element:

var x = document.getElementsByTagName("a")[0];
x.getAttribute("href");
Test and See‹/›

Get the value of the src attribute of the image element:

var x = document.getElementsByTagName("img")[0];
x.getAttribute("src");
Test and See‹/›

Related References

HTML Tutorial:HTML Attributes

HTML DOM Reference:element.hasAttribute() method

HTML DOM Reference:element.setAttribute() method

HTML DOM Reference:element.removeAttribute() method

HTML DOM Element Object