JavaScript NamedNodeMap item() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Method

Definition

The item() method of a NamedNodeMap returns the Attr at a numeric index, or null if the index is out of range (MDN). You usually call it on element.attributes.

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

Syntax

Call map.item. Start with this line.

JavaScript
namedNodeMap.item(index)
CallWhat it does
namedNodeMap.item(index)Primary signature.
map.item(...)Call with the arguments your case needs.

Built into the browser. Call item(...) on a NamedNodeMap — no import required.

Library declarations
JavaScript
namedNodeMap.item(index)

Parameters

ParameterDescription
indexRequired. number. Zero-based position in the map

Return Value

Return ValueDescription
*Node when in range; null when not

JavaScript Examples

Example 1 — MDN Sample

Read attribute names with item() and bracket notation.

JavaScript
const pre = document.querySelector("pre");
// <pre class="foo" id="bar" contenteditable>
const attrMap = pre.attributes;

console.log(attrMap.item(0).name);
console.log(attrMap[1].name);
console.log(attrMap.item(2).name);
Try It Yourself

How It Works

  1. Get the map. pre.attributes is a live NamedNodeMap.
  2. Index 0. item(0) returns the first Attr (MDN).
  3. Bracket alias. attrMap[1] equals item(1) for numbers.
  4. Read .name. Each Attr exposes its attribute name string.

Example 2 — Loop Every Attribute

Walk the map with length and item(i).

JavaScript
const el = document.getElementById("card");
const map = el.attributes;

for (let i = 0; i < map.length; i++) {
  const attr = map.item(i);
  console.log(attr.name + "=" + attr.value);
}
Try It Yourself

How It Works

  1. Read length. map.length is how many Attr nodes exist.
  2. Loop indexes. Valid indexes run 0 … length - 1.
  3. Call item(i). Each step returns one Attr (never out of range here).
  4. Print name=value. A simple inventory of every attribute.

Example 3 — item() vs getNamedItem()

By index vs by name — and null when out of range.

JavaScript
const el = document.getElementById("card"); // id="card" data-role="panel"
const map = el.attributes;

const byIndex = map.item(0);
const byName = map.getNamedItem("data-role");
const oob = map.item(99);

console.log(byIndex && byIndex.name); // e.g. "id"
console.log(byName && byName.value);  // "panel"
console.log(oob);                     // null
Try It Yourself

How It Works

  1. item → position. Useful when walking every attribute in order.
  2. getNamedItem → name. Better when you already know the attribute name.
  3. Out of range → null. item(99) does not throw (MDN).
  4. Always guard. Check for null before reading .name / .value.

Browser Support

NamedNodeMap.item() is Baseline Widely available (MDN: across browsers since July 2015). Safe to use without a polyfill in modern teaching and production code.

✓ Baseline · Widely available

item()

Returns the Attr at a numeric index — or null if out of range. Available on element.attributes in all major browsers.

98% Widely available
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
NamedNodeMap.item() Excellent

Bottom line: Use freely when looping attributes. Prefer getNamedItem(name) when you already know the attribute name.

Frequently Asked Questions

It returns the Attr at the given numeric index, or null if the index is greater than or equal to the map’s length (MDN).
No. MDN marks NamedNodeMap.item() as Baseline Widely available (across browsers since July 2015). It is not Deprecated, Experimental, or Non-standard.
Yes when i is a number. MDN notes that myMap[i] is equivalent to myMap.item(i). A string key like map["id"] uses name lookup instead.
item(index) finds an attribute by position (0, 1, 2…). getNamedItem(name) finds an attribute by its name string.
item() returns null when index is greater than or equal to attributes.length.
When you need to walk every attribute in order — often in a for loop with map.length.

Next: removeNamedItem()

Learn how to remove an Attr from a NamedNodeMap by attribute name.

removeNamedItem() tutorial →

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.

9 people found this page helpful