JavaScript NamedNodeMap getNamedItemNS() Method

Beginner
⏱️ 11 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Baseline Widely available
Instance method

What You’ll Learn

The getNamedItemNS() method of a NamedNodeMap returns a namespaced Attr node when you pass a namespace URI and localName. Learn the MDN XML example, why the URI is not the prefix, and how it compares with getNamedItem() and getAttributeNS()—with five examples and try-it labs.

01

Kind

Instance method

02

Returns

Attr or null

03

Args

URI + localName

04

Not prefix

namespace URI

05

Use case

XML / SVG

06

Status

Baseline widely

Introduction

HTML attributes like id and class usually have no namespace. XML and SVG often do—for example ob:one="test" with xmlns:ob="http://www.example.com/ob".

getNamedItem("ob:one") is not the right tool for that namespaced attribute. Instead, call getNamedItemNS("http://www.example.com/ob", "one") on element.attributes.

💡
Beginner tip

MDN warns: the first argument is the namespace URI (the full URL), not the prefix (ob). Pass "http://www.example.com/ob", not "ob".

Understanding the getNamedItemNS() Method

An instance method on NamedNodeMap that looks up an attribute by namespace URI and local name.

  • namespace — string URI of the attribute’s namespace (not the prefix).
  • localName — string local part of the attribute name (without prefix).
  • Returns — an Attr node, or null if not found.
  • Common sourceelement.attributes on XML/SVG nodes.
  • Pair withDOMParser for in-memory XML (MDN example).
  • Baseline Widely available on MDN (since July 2015).

📝 Syntax

Call the method on a NamedNodeMap (usually from element.attributes):

JavaScript
namedNodeMap.getNamedItemNS(namespace, localName)

Parameters

  • namespace — namespace URI string (use null for attributes with no namespace in some APIs; here pass the actual URI from xmlns).
  • localName — the attribute’s local name without prefix.

Return value

An Attr for the namespace and local name, or null if none matches.

Typical pattern (MDN idea)

JavaScript
const parser = new DOMParser();
const xmlString =
  '<warning ob:one="test" xmlns:ob="http://www.example.com/ob">Beware!</warning>';
const doc = parser.parseFromString(xmlString, "application/xml");

const warning = doc.querySelector("warning");
const value = warning.attributes.getNamedItemNS(
  "http://www.example.com/ob",
  "one",
).value;

console.log(value); // "test"

⚡ Quick Reference

GoalCode / note
Get namespaced Attrel.attributes.getNamedItemNS(uri, "one")
Read valueattr.value after null check
First argNamespace URI (not prefix)
Second arglocalName without prefix
String only?Use getAttributeNS()
MDN statusBaseline Widely available (since July 2015)

🔍 At a Glance

Four facts to remember about NamedNodeMap.getNamedItemNS().

Returns
Attr|null

Node or missing

Args
uri, local

Two strings

URI
not prefix

MDN warning

Baseline
widely

Since Jul 2015

Examples Gallery

Examples follow MDN NamedNodeMap.getNamedItemNS. Labs use DOMParser and namespaced XML attributes.

📚 Getting Started

MDN XML pattern and namespace URI rules.

Example 1 — MDN XML ob:one Attribute

Parse XML and read a namespaced attribute from attributes.

JavaScript
const parser = new DOMParser();
const xmlString =
  '<warning ob:one="test" xmlns:ob="http://www.example.com/ob">Beware!</warning>';
const doc = parser.parseFromString(xmlString, "application/xml");
const warning = doc.querySelector("warning");

const value = warning.attributes.getNamedItemNS(
  "http://www.example.com/ob",
  "one",
).value;

console.log(`The 'ob:one' attribute contains: ${value}.`);
Try It Yourself

How It Works

MDN: URI http://www.example.com/ob + localName one maps to the ob:one attribute.

Example 2 — Read localName from the Attr

The returned Attr exposes namespace metadata.

JavaScript
const doc = new DOMParser().parseFromString(
  '<item app:id="42" xmlns:app="https://example.com/app"/>',
  "application/xml",
);
const item = doc.documentElement;
const attr = item.attributes.getNamedItemNS("https://example.com/app", "id");

console.log({
  localName: attr.localName,
  value: attr.value,
  namespaceURI: attr.namespaceURI,
});
Try It Yourself

How It Works

The prefix in markup (app:id) is not passed to getNamedItemNS—only URI and id.

📈 Edge Cases & Comparisons

Wrong URI, and alternatives for lookup.

Example 3 — null When Namespace URI Is Wrong

Passing the prefix instead of the URI fails lookup.

JavaScript
const doc = new DOMParser().parseFromString(
  '<warning ob:one="test" xmlns:ob="http://www.example.com/ob"/>',
  "application/xml",
);
const warning = doc.documentElement;

const correct = warning.attributes.getNamedItemNS("http://www.example.com/ob", "one");
const wrongPrefix = warning.attributes.getNamedItemNS("ob", "one");

console.log(correct !== null, wrongPrefix === null); // true true
Try It Yourself

How It Works

MDN’s warning: namespace must be the URI, not the prefix string "ob".

Example 4 — getNamedItemNS() vs getNamedItem()

Qualified name lookup does not replace namespace-aware lookup.

JavaScript
const doc = new DOMParser().parseFromString(
  '<warning ob:one="test" xmlns:ob="http://www.example.com/ob"/>',
  "application/xml",
);
const map = doc.documentElement.attributes;

const viaNS = map.getNamedItemNS("http://www.example.com/ob", "one");
const viaName = map.getNamedItem("ob:one"); // may work in some parsers

console.log(viaNS?.value, viaName?.value);
Try It Yourself

How It Works

Prefer getNamedItemNS for portable XML code; do not rely on prefixed names alone.

Example 5 — getNamedItemNS() vs getAttributeNS()

Attr node from the map vs string from the element.

JavaScript
const doc = new DOMParser().parseFromString(
  '<warning ob:one="test" xmlns:ob="http://www.example.com/ob"/>',
  "application/xml",
);
const warning = doc.documentElement;
const uri = "http://www.example.com/ob";

const attr = warning.attributes.getNamedItemNS(uri, "one");
const str = warning.getAttributeNS(uri, "one");

console.log(typeof attr, typeof str, attr.value === str);
Try It Yourself

How It Works

Values match; types differ—use getAttributeNS when you only need the string.

🚀 Common Use Cases

  • Read namespaced XML attributes from DOMParser documents.
  • Inspect SVG or MathML attribute nodes with namespace metadata.
  • Interop with code that walks element.attributes NamedNodeMap.
  • Teach the difference between prefix, localName, and namespace URI.
  • Debug XML configs where attributes use xmlns: declarations.

🔧 How It Works

1

Parse or select element

Often an XML element from DOMParser with xmlns declarations on the node or ancestors.

Context
2

Call getNamedItemNS(uri, local)

Pass the namespace URI and localName—not the prefix from ob:one.

Lookup
3

Return Attr or null

Found: Attr with .value, .localName, .namespaceURI. Not found: null.

Result
4

Read attr.value

Or call element.getAttributeNS(uri, local) when you only need the string value.

📝 Notes

  • MDN: Baseline Widely available (since July 2015) — no Deprecated / Experimental / Non-standard banner.
  • First parameter is namespace URI, not xmlns prefix.
  • Second parameter is localName without prefix.
  • Most plain HTML attributes do not need getNamedItemNS.
  • Related learning: getNamedItem(), getAttributeNS(), Element.attributes.

Universal Browser Support

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

Returns the namespaced Attr for a namespace URI and localName—or null if not found.

Universal Widely available
Google Chrome Full support · Desktop & Mobile
Full support
Mozilla Firefox Full support · Desktop & Mobile
Full support
Apple Safari Full support · macOS & iOS
Full support
Microsoft Edge Full support · Chromium
Full support
Opera Full support · Modern versions
Full support
Internet Explorer Supported on NamedNodeMap (legacy DOM)
Legacy OK
NamedNodeMap.getNamedItemNS() Excellent

Bottom line: Call map.getNamedItemNS(namespaceURI, localName) on element.attributes for XML and SVG namespaced attributes.

Conclusion

NamedNodeMap.getNamedItemNS() looks up namespaced attributes by namespace URI and localName. Use it on XML and SVG nodes, remember the URI is not the prefix, and prefer getAttributeNS() when you only need a string.

Continue with item(), getNamedItem(), getAttributeNS(), or the JavaScript hub.

💡 Best Practices

✅ Do

  • Pass the namespace URI as the first argument
  • Use localName without the prefix
  • Check for null before reading .value
  • Use getAttributeNS() for simple string reads
  • Parse XML with DOMParser for MDN-style demos

❌ Don’t

  • Pass the xmlns prefix where MDN expects a URI
  • Use getNamedItemNS for ordinary HTML id / class
  • Assume getNamedItem("ob:one") is always equivalent
  • Forget null checks on missing namespaced attrs
  • Confuse namespace URI with element tag names

Key Takeaways

Knowledge Unlocked

Five things to remember about getNamedItemNS()

Namespaced Attr lookup by URI + localName.

5
Core concepts
⚙️02

Args

uri, local

Params
⚠️03

Not prefix

use URI

MDN
📄04

XML/SVG

main use

Context
🎯05

Baseline

since Jul 2015

Status

❓ Frequently Asked Questions

An Attr node for the given namespace URI and localName, or null if no matching namespaced attribute exists on the element.
No. MDN marks NamedNodeMap.getNamedItemNS() as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard.
The namespace URI (a full URL string), not the xmlns prefix. MDN warns: namespace is the URI, not the prefix like ob in ob:one.
getNamedItem() looks up by a single name (typical HTML attributes). getNamedItemNS() needs both namespace URI and localName for XML/SVG namespaced attributes.
getNamedItemNS() on attributes returns an Attr node (or null). element.getAttributeNS(namespace, localName) returns the string value (or null).
When working with XML documents, SVG, or other markup where attributes belong to a namespace—e.g. parsed XML from DOMParser.
Did you know?

MDN’s example uses ob:one="test" with xmlns:ob="http://www.example.com/ob", but calls getNamedItemNS("http://www.example.com/ob", "one")—the URI and local name, not the ob prefix.

Explore item()

Walk every attribute by numeric index on NamedNodeMap.

item() method →

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.

5 people found this page helpful