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
Fundamentals
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".
Concept
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 source — element.attributes on XML/SVG nodes.
Pair with — DOMParser for in-memory XML (MDN example).
Baseline Widely available on MDN (since July 2015).
Foundation
📝 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.
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.
UniversalWidely available
Google ChromeFull support · Desktop & Mobile
Full support
Mozilla FirefoxFull support · Desktop & Mobile
Full support
Apple SafariFull support · macOS & iOS
Full support
Microsoft EdgeFull support · Chromium
Full support
OperaFull support · Modern versions
Full support
Internet ExplorerSupported 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.
Wrap Up
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.
Assume getNamedItem("ob:one") is always equivalent
Forget null checks on missing namespaced attrs
Confuse namespace URI with element tag names
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about getNamedItemNS()
Namespaced Attr lookup by URI + localName.
5
Core concepts
🔗01
Returns
Attr|null
API
⚙️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.