Home JavaScript NamedNodeMap JavaScript NamedNodeMap Definition
Definition and Usage A NamedNodeMap is a live collection of attribute nodes (Attr) on an element. You usually get it from element.attributes.
Look up an attribute with getNamedItem("class"), read length, and use item(index) for positional access. It is array-like but not a real Array—there is no forEach on older engines.
This page teaches what NamedNodeMap is. Method and property guides live under this section.
Live collection. If you setAttribute on the element, attributes.length updates. You are not holding a frozen snapshot.
Syntax
JavaScript Syntax Working with element.attributes:
const map = el.attributes;
const idAttr = map.getNamedItem("id");
console.log(map.length);
console.log(idAttr && idAttr.value); Reference
Core Ideas Ideas every NamedNodeMap tutorial builds on:
Idea Example Remember Source el.attributesNamedNodeMap of Attr nodes By name map.getNamedItem("id")Returns Attr or null By index map.item(0)Positional access length map.lengthHow many attributes Live setAttribute …Map updates with the element Not Array Array.from(map)Convert when you need map/filter
Code
JavaScript Examples Three short programs that show how NamedNodeMap works.
1. length 2. getNamedItem 3. item Example 1 — Count attributes length reports how many attributes the element currently has.
const el = document.createElement("input");
el.setAttribute("type", "text");
el.setAttribute("name", "email");
console.log(el.attributes.length); How It Works Two setAttribute calls add type and name.attributes.length becomes 2.Removing an attribute lowers the length. Example 2 — Read an Attr by name getNamedItem returns the Attr node, or null.
el.setAttribute("id", "app");
const attr = el.attributes.getNamedItem("id");
console.log(attr.value); How It Works setAttribute("id", "app") creates the attribute.getNamedItem("id") returns the Attr.value is the string "app".Example 3 — Read by index item(i) returns the Attr at that position.
const first = el.attributes.item(0);
console.log(first && first.name); How It Works item(0) is the first attribute in the map.name is the attribute’s name string.Order is implementation-defined; prefer getNamedItem for lookups. Continue
Next: NamedNodeMap Guides Continue with getNamedItem and the other map methods.
Look up Why getNamedItem() Find an attribute by name setNamedItem()Add or replace an Attr removeNamedItem()Remove by name lengthCount attributes
Full lists: getNamedItem() .
Frequently Asked Questions What is a NamedNodeMap? A NamedNodeMap is the live collection of attribute nodes on an element, usually read from element.attributes.
Is NamedNodeMap an array? No. It is array-like: it has length and item(index), but it is not a real Array. Use Array.from(map) when you need array methods.
Should I use attributes or getAttribute? getAttribute/setAttribute are simpler for everyday string attributes. Use the NamedNodeMap when you need Attr nodes or to list every attribute.
Is the map live? Yes. Changing attributes on the element updates the map’s contents and length.
Where should I go next? Open getNamedItem(), then Element attribute methods for everyday use.
Open getNamedItem() Then explore setNamedItem, removeNamedItem, and length.
getNamedItem() → CODETOFUN NEWS
Stay Ahead in Tech Get the latest IT news, programming updates, and technology trends directly on your phone.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
12 people found this page helpful
Helpful Share Copy link Suggestion