JavaScript NamedNodeMap

Beginner
8 min read
Updated: Nov 2026
3 examples
Attributes map

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.

JavaScript Syntax

Working with element.attributes:

JavaScript
const map = el.attributes;
const idAttr = map.getNamedItem("id");
console.log(map.length);
console.log(idAttr && idAttr.value);

Core Ideas

Ideas every NamedNodeMap tutorial builds on:

IdeaExampleRemember
Sourceel.attributesNamedNodeMap of Attr nodes
By namemap.getNamedItem("id")Returns Attr or null
By indexmap.item(0)Positional access
lengthmap.lengthHow many attributes
LivesetAttribute …Map updates with the element
Not ArrayArray.from(map)Convert when you need map/filter

JavaScript Examples

Three short programs that show how NamedNodeMap works.

Example 1 — Count attributes

length reports how many attributes the element currently has.

JavaScript
const el = document.createElement("input");
el.setAttribute("type", "text");
el.setAttribute("name", "email");
console.log(el.attributes.length);

How It Works

  1. Two setAttribute calls add type and name.
  2. attributes.length becomes 2.
  3. Removing an attribute lowers the length.

Example 2 — Read an Attr by name

getNamedItem returns the Attr node, or null.

JavaScript
el.setAttribute("id", "app");
const attr = el.attributes.getNamedItem("id");
console.log(attr.value);

How It Works

  1. setAttribute("id", "app") creates the attribute.
  2. getNamedItem("id") returns the Attr.
  3. value is the string "app".

Example 3 — Read by index

item(i) returns the Attr at that position.

JavaScript
const first = el.attributes.item(0);
console.log(first && first.name);

How It Works

  1. item(0) is the first attribute in the map.
  2. name is the attribute’s name string.
  3. Order is implementation-defined; prefer getNamedItem for lookups.

Next: NamedNodeMap Guides

Continue with getNamedItem and the other map methods.

Look upWhy
getNamedItem()Find an attribute by name
setNamedItem()Add or replace an Attr
removeNamedItem()Remove by name
lengthCount attributes

Full lists: getNamedItem().

Frequently Asked Questions

A NamedNodeMap is the live collection of attribute nodes on an element, usually read from element.attributes.
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.
getAttribute/setAttribute are simpler for everyday string attributes. Use the NamedNodeMap when you need Attr nodes or to list every attribute.
Yes. Changing attributes on the element updates the map’s contents and length.
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.

Your daily dose of tech, straight to your feed.

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.

12 people found this page helpful