JavaScript NamedNodeMap getNamedItemNS() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Method

Definition

The getNamedItemNS() method returns the Attr for a given namespace URI and local name, or null if it is missing (MDN). 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.getNamedItemNS. Start with this line.

JavaScript
map.getNamedItemNS(namespace, localName)
CallWhat it does
map.getNamedItemNS(namespace, localName)Primary signature.
map.getNamedItemNS(...)Call with the arguments your case needs.

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

Library declarations
JavaScript
map.getNamedItemNS(namespace, localName)

Parameters

ParameterDescription
namespaceRequired. string. Namespace URI (not the prefix)
localNameRequired. string. Local name without prefix

Return Value

Return ValueDescription
*Node when found; null when missing

JavaScript Examples

Example 1 — MDN XML Sample

Parse XML and read the ob:one attribute via its namespace URI.

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

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

console.log(value); // "test"
Try It Yourself

How It Works

  1. Parse XML. DOMParser builds a document with real namespaces.
  2. Select the element. warning owns the ob:one Attr.
  3. Pass URI + local name. "http://www.example.com/ob" and "one" (MDN).
  4. Read .value. The Attr’s string value is "test".

Example 2 — null for Wrong URI or Missing Name

A prefix is not a URI — and a missing local name returns null.

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

const ok = map.getNamedItemNS("http://www.example.com/ob", "one");
const badUri = map.getNamedItemNS("ob", "one");
const missing = map.getNamedItemNS("http://www.example.com/ob", "two");

console.log(ok && ok.value); // "test"
console.log(badUri);         // null
console.log(missing);        // null
Try It Yourself

How It Works

  1. Correct pair works. Full URI + "one" finds the Attr.
  2. Prefix as URI fails. "ob" is not the namespace URI (MDN warning).
  3. Wrong local name fails. "two" is not on the element → null.
  4. Always guard. Check for null before reading .value.

Example 3 — getNamedItemNS() vs getAttributeNS()

Attr node vs plain string — same lookup keys, different return types.

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

const attr = el.attributes.getNamedItemNS(ns, "one");
const str = el.getAttributeNS(ns, "one");

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

How It Works

  1. Same keys. Both APIs take namespace URI + local name.
  2. getNamedItemNS → Attr. Use when you need the node (name, URI, …).
  3. getAttributeNS → string. Simpler when you only need the value.
  4. Values match when present. attr.value equals the string.

Browser Support

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

getNamedItemNS()

Returns the Attr for a namespace URI + local 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.getNamedItemNS() Excellent

Bottom line: Use freely for XML/SVG namespaces. Prefer getAttributeNS() when you only need a string; use getNamedItem() for plain HTML names.

Frequently Asked Questions

It returns the Attr for the given namespace URI and local name, or null if no matching namespaced attribute exists (MDN).
No. MDN marks NamedNodeMap.getNamedItemNS() as Baseline Widely available (across browsers since July 2015). It is not Deprecated, Experimental, or Non-standard.
A namespace URI — not the xmlns prefix. MDN warns: namespace is the URI of the namespace, not the prefix (for example use http://www.example.com/ob, not ob).
getNamedItem(name) looks up by a single name (typical HTML). getNamedItemNS(namespace, localName) needs both the namespace URI and the local name for XML/SVG attributes.
getNamedItemNS() returns an Attr node (or null). element.getAttributeNS(namespace, localName) returns the string value (or null).
When working with XML, SVG, or other markup where attributes belong to a namespace — often after parsing XML with DOMParser.

Next: item()

Learn how to read Attr nodes from a NamedNodeMap by numeric index.

item() 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.

8 people found this page helpful