Element: getAttributeNode() method

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

The getAttributeNode() method of the Element interface returns the specified attribute of the specified element, as an Attr node. It returns null if the element doesn't have an attribute with the given name.

This method is useful if you need the attribute's instance properties. If you only need the attribute's value, you can use the getAttribute() method instead.

Syntax

js
getAttributeNode(attrName)

Parameters

attrName

A string specifying the name of the attribute. When called on an HTML element in a DOM flagged as an HTML document, the name is normalized to lowercase.

Return value

An Attr node for the attribute, or null if the element doesn't have an attribute with the given name.

Note: The Attr node inherits from Node, but is not considered a part of the document tree. Common Node attributes like parentNode, previousSibling, and nextSibling are null for an Attr node. You can, however, get the element to which the attribute belongs with the ownerElement property.

Examples

js
// html: <div id="top" />
const t = document.getElementById("top");
const idAttr = t.getAttributeNode("id");
console.log(idAttr.value); // "top"

Specifications

Specification
DOM
# dom-element-getattributenode

Browser compatibility

See also