HTML DOM Element getAttributeNode()
Example
Get the value of the class attribute node of an <h1> element:
const element = document.getElementsByTagName("H1")[0];
let text = element.getAttributeNode("class").value;
Try it Yourself »
More examples below.
Definition and Usage
The getAttributeNode()
method returns an element's attribute.
The getAttributeNode()
method returns an Attribute object.
The Difference Between getAttribute() and getAttributeNode()
The getAttribute()
method returns the value of an attribute.
The getAttributeNode()
method returns an Attr object,
and you must use the Attr value property to get the value.
The result will be the same.
Alternative:
It is easier to use The getAttribute() Method
See Also:
The removeAttributeNode() method
Tutorial:
Reference:
Syntax
element.getAttributeNode(name)
Parameters
Parameter | Description |
name | Required. The name of the attribute. |
Return Value
Type | Description |
Object | The Attr object of the attribute node.null if the attribute does not exist. |
More Examples
Example
Get the value of the target attribute node of an <a> element:
var elmnt = document.getElementById("myAnchor");
var attr = elmnt.getAttributeNode("target").value;
Try it Yourself »
Example
Get the value of the onclick attribute node of a <button> element:
var elmnt = document.getElementById("myBtn");
var attr = elmnt.getAttributeNode("onclick").value;
Try it Yourself »
Browser Support
element.getAttributeNode()
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 |