HTML DOM Element getAttribute() method
- Previous Page focus()
- Next Page getAttributeNode()
- Go to Parent Page HTML DOM Elements Object
Definition and Usage
getAttribute()
The method returns the value of the element's attribute.
See also:
Tutorial:
Example
Example 1
Get the class attribute value of the element:
let text = element.getAttribute("class");
Example 2
Get the value of the target attribute of the <a> element:
let text = myAnchor.getAttribute("target");
Example 3
Get the value of the onclick attribute of the <button> element:
let text = myButton.getAttribute("onclick");
Syntax
element.getAttribute(name)
Parameter
Parameter | Description |
---|---|
name | Required. The name of the attribute. |
Return Value
Type | Description |
---|---|
String | The value of the attribute. |
null | If the attribute does not exist. |
Technical Details
The HTMLElement object defines JavaScript properties corresponding to each standard HTML attribute, so you only need to use this method with the HTML document when querying the value of non-standard attributes.
In XML documents, attribute values cannot be used directly as element attributes and must be queried by calling a method. For XML documents using namespaces, you need to use getAttributeNS() Method.
Browser Support
element.getAttribute
It is a DOM Level 1 (1998) feature.
All browsers fully support it:
Chrome | IE | Edge | Firefox | Safari | Opera |
---|---|---|---|---|---|
Chrome | IE | Edge | Firefox | Safari | Opera |
Support | 9-11 | Support | Support | Support | Support |
- Previous Page focus()
- Next Page getAttributeNode()
- Go to Parent Page HTML DOM Elements Object