HTML DOM Element getAttribute()
Examples
Get the value of the class attribute of an element:
let text = element.getAttribute("class");
Try it Yourself »
Get the value of the target attribute of an <a> element:
let text = myAnchor.getAttribute("target");
Try it Yourself »
More examples below.
Definition and Usage
The getAttribute()
method returns the value of an element's attribute.
Syntax
element.getAttribute(name)
Parameters
Parameter | Description |
name | Required. The name of the attribute. |
Return Value
Type | Description |
String | The attribute's value.null if the attribute does not exist. |
More Examples
Get the value of the onclick attribute of a <button> element:
let text = myButton.getAttribute("onclick");
Try it Yourself »
Browser Support
element.getAttribute
is a DOM Level 1 (1998) feature.
It is fully supported in all browsers:
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | 9-11 | Yes | Yes | Yes | Yes |