HTML DOM Element getAttributeNode() method

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:

setAttribute() method

hasAttribute() method

removeAttribute() method

setAttributeNode() method

removeAttributeNode() method

Tutorial:

HTML Attributes

Reference Manual:

HTML DOM Attribute Object

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;

Try it yourself

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;

Try it yourself

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;

Try it yourself

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