English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
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.
element.getAttribute(attr)
var x = document.getElementsByTagName("H1")[0].getAttribute("class");Test and See‹/›
All browsers fully support the getAttribute() method:
Method | |||||
getAttribute() | Is | Is | Is | Is | Is |
Parameter | Description |
---|---|
attr | The name of the attribute from which you want to retrieve the value |
Return value: | A string that represents the value of the specified attribute |
---|---|
DOM Version: | DOM Level1 |
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‹/›
HTML Tutorial:HTML Attributes
HTML DOM Reference:element.hasAttribute() method
HTML DOM Reference:element.setAttribute() method
HTML DOM Reference:element.removeAttribute() method