Element.getAttributeNodeNS() is an instance method that returns the Attr node for a namespaced attribute. Learn namespace URI + qualified name, how it compares to getAttributeNode() and getAttributeNS(), common SVG xlink:href usage, and five try-it labs aligned with MDN.
01
Kind
Instance method
02
Returns
Attr or null
03
Args
namespace, name
04
Scope
Namespaced attrs
05
vs getAttrNS
Attr vs string
06
Status
Baseline widely
Fundamentals
Introduction
Plain HTML attributes like id or class usually work with getAttribute() or getAttributeNode(). SVG and XML documents also use namespaced attributes such as xlink:href, which belong to a namespace URI.
getAttributeNodeNS(namespace, nodeName) looks up that namespaced attribute and returns the full Attr object—not just the string value.
💡
Beginner tip
If you only need the value, getAttributeNS(namespace, name) is simpler. Use getAttributeNodeNS() when you need Attr properties like namespaceURI or ownerElement (MDN).
Calling el.getAttributeNodeNS(namespace, nodeName) finds an attribute that matches both the namespace URI and the qualified name, then returns its Attr node.
It is an instance method on Element.
namespace — the attribute’s namespace URI (e.g. XLink namespace).
nodeName — the qualified name (e.g. "xlink:href").
Returns an Attr node, or null if not found.
More specific than getAttributeNode() for namespaced attributes (MDN).
Setter counterpart: setAttributeNodeNS() (MDN).
Foundation
📝 Syntax
General form of Element.getAttributeNodeNS (MDN):
JavaScript
getAttributeNodeNS(namespace, nodeName)
Parameters
namespace — a string specifying the namespace URI of the attribute.
nodeName — a string specifying the qualified name of the attribute.
Return value
The Attr node for the specified namespaced attribute, or null if it does not exist.
Common patterns
JavaScript
const XLINK = "http://www.w3.org/1999/xlink";
const link = document.querySelector("svg a");
const hrefAttr = link.getAttributeNodeNS(XLINK, "xlink:href");
if (hrefAttr) {
console.log(hrefAttr.value);
console.log(hrefAttr.namespaceURI);
}
// Value only? Use getAttributeNS:
console.log(link.getAttributeNS(XLINK, "xlink:href"));
Cheat Sheet
⚡ Quick Reference
Goal
Code
Namespaced Attr node
el.getAttributeNodeNS(ns, "xlink:href")
Read value from Attr
attr.value
Namespaced string value
el.getAttributeNS(ns, "xlink:href")
Non-namespaced HTML attr
el.getAttributeNode("id")
Not found
null
MDN status
Baseline Widely available (since July 2015)
Snapshot
🔍 At a Glance
Four facts to remember about Element.getAttributeNodeNS().
Element.getAttributeNodeNS() is Baseline Widely available (MDN: across browsers since July 2015). Logos use the shared browser-image-sprite.png sprite from this project.
✓ Baseline Widely available
Element.getAttributeNodeNS()
Get namespaced Attr nodes for SVG and XML — use getAttributeNS() for simple value reads.
BaselineWidely available
Google ChromeSupported · Desktop & Mobile
Yes
Microsoft EdgeSupported · Chromium
Yes
Mozilla FirefoxSupported · Desktop & Mobile
Yes
Apple SafariSupported · macOS & iOS
Yes
OperaSupported · Modern versions
Yes
Internet ExplorerSupported in legacy IE
Yes
getAttributeNodeNS()Excellent
Bottom line: Use getAttributeNodeNS() for namespaced Attr inspection in SVG/XML. Pass the correct namespace URI and qualified name; prefer getAttributeNS() when you only need the string value.
Wrap Up
Conclusion
Element.getAttributeNodeNS() is the namespaced version of getAttributeNode(). Pass a namespace URI and qualified name to get an Attr node from SVG or XML documents.
Use the correct namespace URI (not just the prefix)
Null-check before reading attr.value
Prefer getAttributeNS() for value-only reads
Use getAttributeNode() for plain HTML attrs
Pair with setAttributeNS() when creating attrs
❌ Don’t
Pass "xlink" instead of the full namespace URI
Use for ordinary id / class on HTML
Assume return type is a string
Confuse qualified name with local name only
Forget SVG needs createElementNS for elements too
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about Element.getAttributeNodeNS()
Namespaced Attr nodes for SVG and XML.
5
Core concepts
📝01
Returns
Attr | null
API
📄02
Args
URI + name
Two
✍️03
SVG/XML
namespaced
Scope
✅04
Baseline
widely available
Status
⚡05
Values
getAttributeNS
Simpler
❓ Frequently Asked Questions
It returns the Attr node for a namespaced attribute on an element. You pass a namespace URI and the qualified attribute name (for example xlink:href).
No. MDN marks Element.getAttributeNodeNS() as Baseline Widely available (across browsers since July 2015). It is not Deprecated, Experimental, or Non-standard.
Two strings: namespace (the attribute namespace URI) and nodeName (the qualified name, often prefix:localName like xlink:href).
getAttributeNS() returns the attribute value as a string (or null). getAttributeNodeNS() returns the Attr object so you can read name, value, namespaceURI, and ownerElement.
For non-namespaced attributes in HTML documents, use getAttributeNode(). Use getAttributeNodeNS() when the attribute belongs to a specific namespace (common in SVG and XML).
The method returns null, same as other attribute lookup methods when nothing matches.
Did you know?
MDN notes that getAttributeNodeNS() is more specific than getAttributeNode() because it targets attributes in a particular namespace. For everyday HTML id and class attributes, the non-NS method is the right tool.