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.


Description

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.




Syntax

element.getAttributeNode(name)

Parameters

Parameter Description
name Required.
The name of the attribute.

Return Value

Type Description
ObjectThe 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 Edge Firefox Safari Opera IE
Yes Yes Yes Yes Yes 9-11

Copyright 1999-2023 by Refsnes Data. All Rights Reserved.