JavaScript NamedNodeMap getNamedItem() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Method

Definition

The getNamedItem() method of a NamedNodeMap returns the Attr for a given name, or null if it is missing (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.getNamedItem. Start with this line.

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

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

Library declarations
JavaScript
namedNodeMap.getNamedItem(name)

Parameters

ParameterDescription
nameRequired. string. Attribute name to find (e.g. "id", "class")

Return Value

Return ValueDescription
*Node when found; null when missing

JavaScript Examples

Example 1 — MDN Sample

Read a present attribute and check a missing name with brackets.

JavaScript
const pre = document.querySelector("pre"); // <pre test="test">
const attrMap = pre.attributes;
const value = attrMap.getNamedItem("test").value;

console.log("test =", value);
console.log("foo found?", Boolean(attrMap["foo"]));
Try It Yourself

How It Works

  1. Get the map. pre.attributes is a live NamedNodeMap.
  2. Look up by name. getNamedItem("test") returns the Attr (MDN).
  3. Read the value. .value is the string "test".
  4. Missing → falsy. attrMap["foo"] is null, so the check is false.

Example 2 — Read Attr name and value

Fetch the Attr node, then inspect its fields.

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

console.log(idAttr.name);   // "id"
console.log(idAttr.value);  // "card"
Try It Yourself

How It Works

  1. Find the element. Here the node has id="card".
  2. Call getNamedItem. Pass the attribute name string "id".
  3. Get an Attr object. Not just a string — a full node.
  4. Read fields. .name and .value describe that attribute.

Example 3 — getNamedItem() vs getAttribute()

Attr node vs plain string — and a safe null check.

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

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

How It Works

  1. getNamedItem → Attr or null. Useful when you need the node.
  2. getAttribute → string or null. Simpler when you only need the value.
  3. Values match when present. attr.value equals the string from getAttribute.
  4. Always guard null. Missing names return null — do not read .value blindly.

Browser Support

NamedNodeMap.getNamedItem() 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

getNamedItem()

Returns the Attr for a given attribute name — or null if missing. 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.getNamedItem() Excellent

Bottom line: Use freely in modern apps. Prefer getAttribute() when you only need a string value; use getNamedItem() when you need the Attr node.

Frequently Asked Questions

It returns the Attr node for the given attribute name, or null if that name is not on the map (MDN).
No. MDN marks NamedNodeMap.getNamedItem() as Baseline Widely available (across browsers since July 2015). It is not Deprecated, Experimental, or Non-standard.
Most often from element.attributes — a live map of every Attr node on the element.
Yes. MDN notes that bracket syntax with a string key is equivalent to getNamedItem(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.

Next: getNamedItemNS()

Learn namespaced attribute lookup with a namespace URI and local name.

getNamedItemNS() 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