JavaScript Element getAttributeNode() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Method

Definition

The Element.getAttributeNode() method returns a named attribute as an Attr node (MDN). Missing attribute → null. If you only need the string value, prefer getAttribute().

Pass the required arguments shown below. Check return values and browser support before you rely on the result.

Syntax

Call el.getAttributeNode. Start with this line.

JavaScript
el.getAttributeNode(attrName)
CallWhat it does
el.getAttributeNode(attrName)Primary signature.
el.getAttributeNode(...)Call with the arguments your case needs.

Built into the browser. Call getAttributeNode(...) on an Element — no import required.

Library declarations
JavaScript
el.getAttributeNode(attrName)

Parameters

ParameterDescription
attrNameRequired. Attribute name (HTML → lowercase)

Return Value

Return ValueDescription
*Attr node, or null

JavaScript Examples

Example 1 — Read Attr.value

Get the id attribute as an Attr, then read its value.

JavaScript
const t = document.getElementById("top");
const out = document.getElementById("out");

const idAttr = t.getAttributeNode("id");
out.textContent = idAttr.value;
Try It Yourself

How It Works

  1. Select the element. Same starting point as getAttribute.
  2. Ask for the Attr. getAttributeNode("id") returns a node.
  3. Read .value. That is the attribute’s string content.
  4. Shortcut: getAttribute("id") if you only need that string.

Example 2 — Missing → null

When the attribute is not present, there is no Attr node.

JavaScript
const t = document.getElementById("top");
const out = document.getElementById("out");

const langAttr = t.getAttributeNode("lang");
out.textContent = langAttr === null ? "null" : langAttr.value;
Try It Yourself

How It Works

  1. Request a missing name. This element has no lang.
  2. Receive null. Same missing rule as getAttribute.
  3. Null-check first. Reading .value on null throws.
  4. Then use the node. Safe after the check.

Example 3 — ownerElement

An Attr is not in the tree — use ownerElement to find its element (MDN).

JavaScript
const t = document.getElementById("top");
const out = document.getElementById("out");

const idAttr = t.getAttributeNode("id");
out.textContent =
  "owner id: " + idAttr.ownerElement.id + "\n" +
  "parentNode: " + String(idAttr.parentNode);
Try It Yourself

How It Works

  1. Get the Attr. Same call as example 1.
  2. Read ownerElement. Points back to the element.
  3. Check parentNode. MDN: null for Attr nodes.
  4. Use case: APIs that pass around Attr objects, not strings.

Browser Support

Element.getAttributeNode() is Baseline Widely available on MDN (across browsers since July 2015). It is not Deprecated, Experimental, or Non-standard.

✓ Baseline Widely available

Element.getAttributeNode()

Return a named attribute as an Attr node. Core DOM — available everywhere modern.

2015+ Widely available
Google Chrome All modern
Supported
Mozilla Firefox All modern
Supported
Apple Safari All modern
Supported
Microsoft Edge All modern
Supported
Opera All modern
Supported
Internet Explorer Supported
Supported
getAttributeNode() Baseline

Bottom line: Safe for all modern browsers and IE. Prefer getAttribute() unless you need the Attr object.

Frequently Asked Questions

It returns the specified attribute as an Attr node object, not a plain string (MDN). Use it when you need Attr properties such as name, value, or ownerElement.
No. MDN marks Element.getAttributeNode() as Baseline Widely available (across browsers since July 2015). It is not Deprecated, Experimental, or Non-standard.
null. If no attribute matches the name, there is no Attr node to return.
getAttribute() returns a string value or null. getAttributeNode() returns an Attr object (or null) so you can read node-level details like ownerElement.
No. MDN: parentNode, previousSibling, and nextSibling are null on Attr nodes. Use ownerElement to get the element the attribute belongs to.
When you only need the attribute value as a string, getAttribute() is simpler and what most modern code uses (MDN).

Next: getAttributeNodeNS()

Get a namespaced attribute as an Attr node.

getAttributeNodeNS() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

17 people found this page helpful