HTML DOM Element getAttributeNode() method
- Previous page getAttribute()
- Next page getBoundingClientRect()
- Go back to the previous level HTML DOM Elements Object
Definition and Usage
getAttribute()
The method returns the attribute value of the specified attribute name as an Attr object.
Alternative Solution:
Use getAttribute() method It will be easier.
See also:
Tutorial:
Reference Manual:
Knowledge Point: The difference between getAttribute() and getAttributeNode()
getAttribute()
The method returns the value of the attribute.
getAttributeNode()
method returns Attr object, you must use Attr value attribute to get this value.
The results are the same.
Instance
Example 1
Get the value of the class attribute node of the <h1> element:
const element = document.getElementsByTagName("H1")[0]; let text = element.getAttributeNode("class").value;
Example 2
Get the value of the target attribute node of the <a> element:
var elmnt = document.getElementById("myAnchor"); var attr = elmnt.getAttributeNode("target").value;
Example 3
Get the value of the onclick attribute node of the <button> element:
var elmnt = document.getElementById("myBtn"); var attr = elmnt.getAttributeNode("onclick").value;
Syntax
element.getAttributeNode(name)
Parameter
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. |
Description
getAttributeNode()
The method will return an Attr node representing the value of the specified attribute. Note that the Attr node can also be obtained through the attributes property inherited from the Node interface.
Browser support
element.getAttributeNode()
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 getAttribute()
- Next page getBoundingClientRect()
- Go back to the previous level HTML DOM Elements Object