JavaScript NamedNodeMap getNamedItem() Method

Beginner
⏱️ 10 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Baseline Widely available
Instance method

What You’ll Learn

The getNamedItem() method of a NamedNodeMap returns the Attr node for a given attribute name, or null if it is missing. Learn how it works on element.attributes, bracket notation, and how it compares with getAttribute()—with five examples and try-it labs.

01

Kind

Instance method

02

Returns

Attr or null

03

Arg

name string

04

Source

attributes

05

Alias

map[name]

06

Status

Baseline widely

Introduction

A NamedNodeMap is a collection of nodes keyed by name. On the web, you most often meet it as element.attributes—a live map of every Attr on an element.

To fetch one attribute node by name, call map.getNamedItem("id"). MDN also notes that map["id"] does the same thing when the key is a string.

💡
Beginner tip

For everyday string values, element.getAttribute("id") is usually simpler. Use getNamedItem() when you need the full Attr node—for example its name, value, or specified flag.

Understanding the getNamedItem() Method

An instance method on NamedNodeMap that looks up a node by attribute name.

  • Parametername: a string with the attribute name.
  • Returns — an Attr node, or null if not found.
  • Bracket aliasmap[name] is equivalent (MDN).
  • Common sourceelement.attributes NamedNodeMap.
  • Live map — reflects attribute changes on the element.
  • Baseline Widely available on MDN (since July 2015).

📝 Syntax

Call the method on a NamedNodeMap (usually from element.attributes):

JavaScript
namedNodeMap.getNamedItem(name)

// equivalent bracket form (MDN):
namedNodeMap[name]

Parameters

name — a string: the attribute name to look up (for example "id", "class", "data-role").

Return value

An Attr corresponding to name, or null if no match exists.

Typical pattern (MDN idea)

JavaScript
const pre = document.querySelector("pre");
const attrMap = pre.attributes;
const value = attrMap.getNamedItem("test").value;
pre.textContent = `The 'test' attribute contains ${value}.
And 'foo' has ${attrMap["foo"] ? "been" : "not been"} found.`;

⚡ Quick Reference

GoalCode / note
Get Attr nodeel.attributes.getNamedItem("id")
Read valueattr.value after null check
Bracket formel.attributes["class"]
Missing attrReturns null
String only?Use getAttribute() instead
MDN statusBaseline Widely available (since July 2015)

🔍 At a Glance

Four facts to remember about NamedNodeMap.getNamedItem().

Returns
Attr|null

Node or missing

Arg
name

String key

Alias
map[name]

Bracket OK

Baseline
widely

Since Jul 2015

Examples Gallery

Examples follow MDN NamedNodeMap.getNamedItem. Labs use element.attributes and real HTML attributes.

📚 Getting Started

MDN pattern and basic lookups.

Example 1 — MDN Sample with test Attribute

Read an attribute value from the map and check a missing name with brackets.

JavaScript
const pre = document.querySelector("pre");
const attrMap = pre.attributes;
const value = attrMap.getNamedItem("test").value;
pre.textContent = `The 'test' attribute contains ${value}.
And 'foo' has ${attrMap["foo"] ? "been" : "not been"} found.`;
Try It Yourself

How It Works

MDN’s example: getNamedItem("test") returns the Attr; attrMap["foo"] is falsy when foo is absent.

Example 2 — Look Up id on an Element

Fetch the Attr node and read its name and value.

JavaScript
const el = document.getElementById("card");
const idAttr = el.attributes.getNamedItem("id");

console.log({
  name: idAttr.name,
  value: idAttr.value,
});
Try It Yourself

How It Works

getNamedItem returns the Attr object, not just the string.

📈 Aliases & Edge Cases

Bracket syntax, missing attributes, and comparisons.

Example 3 — Bracket Notation map["class"]

MDN: string bracket access is equivalent to getNamedItem.

JavaScript
const el = document.querySelector(".note");
const map = el.attributes;

const viaMethod = map.getNamedItem("class");
const viaBracket = map["class"];

console.log(viaMethod === viaBracket); // true
Try It Yourself

How It Works

Both forms return the same Attr reference when the attribute exists.

Example 4 — null When Attribute Is Missing

Always check before reading .value.

JavaScript
const el = document.getElementById("card");
const missing = el.attributes.getNamedItem("data-missing");

console.log(missing); // null
Try It Yourself

How It Works

MDN: returns null when no attribute matches the name—unlike some APIs that return undefined.

Example 5 — getNamedItem() vs getAttribute()

Attr node vs plain string value.

JavaScript
const el = document.getElementById("card");
const attr = el.attributes.getNamedItem("data-role");
const str = el.getAttribute("data-role");

console.log(typeof attr);  // "object" (Attr)
console.log(typeof str);   // "string"
console.log(attr.value === str); // true
Try It Yourself

How It Works

Values match, but types differ—getAttribute is usually enough when you only need the string.

🚀 Common Use Cases

  • Read an Attr node from element.attributes by name.
  • Interop with legacy DOM code that walks NamedNodeMap entries.
  • Inspect attribute metadata beyond the string value.
  • Check presence with truthiness on map[name] (prefer hasAttribute for clarity).
  • Teach the difference between NamedNodeMap and Array collections.

🔧 How It Works

1

Get NamedNodeMap

Usually const map = element.attributes—a live collection of Attr nodes.

Source
2

Call getNamedItem(name)

Browser searches the map for an Attr with that attribute name.

Lookup
3

Return Attr or null

Found: Attr node. Not found: null—check before using .value.

Result
4

Read .value or use getAttribute

Use attr.value for the string, or call getAttribute() when you only need the value.

📝 Notes

  • MDN: Baseline Widely available (since July 2015) — no Deprecated / Experimental / Non-standard banner.
  • map["name"] is equivalent to map.getNamedItem("name") for string keys.
  • NamedNodeMap is not an Array—no map / forEach on the collection itself.
  • element.getAttributeNode(name) is another way to get the same Attr.
  • Related learning: Element.attributes, getAttribute(), hasAttributes().

Universal Browser Support

NamedNodeMap.getNamedItem() is marked Baseline Widely available on MDN (since July 2015). Logos use the shared browser-image-sprite.png sprite from this project.

Baseline · Widely available

NamedNodeMap.getNamedItem()

Returns the Attr node for a given attribute name—or null if the attribute is not on the element.

Universal Widely available
Google Chrome Full support · Desktop & Mobile
Full support
Mozilla Firefox Full support · Desktop & Mobile
Full support
Apple Safari Full support · macOS & iOS
Full support
Microsoft Edge Full support · Chromium
Full support
Opera Full support · Modern versions
Full support
Internet Explorer Supported on NamedNodeMap (legacy DOM)
Legacy OK
NamedNodeMap.getNamedItem() Excellent

Bottom line: Call map.getNamedItem(name) on element.attributes to look up Attr nodes by name.

Conclusion

NamedNodeMap.getNamedItem() looks up an attribute by name and returns an Attr node or null. Use it on element.attributes, remember the map[name] alias, and prefer getAttribute() when you only need a string.

Continue with getNamedItemNS(), getAttribute(), Element.attributes, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Check for null before reading .value
  • Use getAttribute() when you only need a string
  • Remember map[name] equals getNamedItem(name)
  • Start from element.attributes for element attrs
  • Use hasAttribute() for simple presence checks

❌ Don’t

  • Assume getNamedItem always returns an object
  • Treat NamedNodeMap like an Array
  • Forget that the map is live after setAttribute
  • Use getNamedItem when getAttribute is enough
  • Read .value on null without a guard

Key Takeaways

Knowledge Unlocked

Five things to remember about getNamedItem()

Look up Attr nodes by name.

5
Core concepts
⚙️02

Arg

name string

Param
📝03

Alias

map[name]

Syntax
📄04

Source

attributes

Map
🎯05

Baseline

since Jul 2015

Status

❓ Frequently Asked Questions

An Attr node matching the given attribute name, or null if no attribute with that name exists on the element.
No. MDN marks NamedNodeMap.getNamedItem() as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard.
Most often from element.attributes—a live map of all Attr nodes on an element.
Yes. MDN notes that bracket syntax with a string is equivalent to calling getNamedItem() with that name.
getNamedItem() returns an Attr node (or null). getAttribute() returns the attribute value as a string (or null).
Yes. If the attribute is missing, getNamedItem() returns null—always check before reading .name or .value.
Did you know?

MDN documents that myMap[str] is equivalent to myMap.getNamedItem(str) when str is a string. That is why both forms appear in the official example.

Explore getNamedItemNS()

Learn namespaced attribute lookup with namespace URI and localName.

getNamedItemNS() method →

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.

5 people found this page helpful