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
Fundamentals
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.
Concept
Understanding the getNamedItem() Method
An instance method on NamedNodeMap that looks up a node by attribute name.
Parameter — name: a string with the attribute name.
Returns — an Attr node, or null if not found.
Bracket alias — map[name] is equivalent (MDN).
Common source — element.attributes NamedNodeMap.
Live map — reflects attribute changes on the element.
Baseline Widely available on MDN (since July 2015).
Foundation
📝 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.`;
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.
UniversalWidely available
Google ChromeFull support · Desktop & Mobile
Full support
Mozilla FirefoxFull support · Desktop & Mobile
Full support
Apple SafariFull support · macOS & iOS
Full support
Microsoft EdgeFull support · Chromium
Full support
OperaFull support · Modern versions
Full support
Internet ExplorerSupported 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.
Wrap Up
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.