Element.getAttributeNS() is an instance method that returns the string value of a namespaced attribute. Learn namespace URI + qualified name, how it compares to getAttribute() and getAttributeNodeNS(), common SVG xlink:href usage, and five try-it labs aligned with MDN.
01
Kind
Instance method
02
Returns
string or null
03
Args
namespace, name
04
Scope
Namespaced attrs
05
vs getAttr
NS + URI needed
06
Status
Baseline widely
Fundamentals
Introduction
Plain HTML attributes like id or class usually work with getAttribute(). SVG and XML documents also use namespaced attributes such as xlink:href, which belong to a namespace URI.
getAttributeNS(namespace, name) looks up that namespaced attribute and returns its value as a string—or null if it does not exist.
💡
Beginner tip
MDN recommends getAttribute() for ordinary HTML attributes when you do not need a namespace. Use getAttributeNS() for SVG/XML namespaced attrs. Need the full Attr object? Use getAttributeNodeNS() instead.
Calling el.getAttributeNS(namespace, name) finds an attribute that matches both the namespace URI and the qualified name, then returns its value as a string.
It is an instance method on Element.
namespace — the attribute’s namespace URI (e.g. XLink namespace).
name — the qualified name (e.g. "xlink:href").
Returns a string, or null if not found (DOM4; MDN).
More specific than getAttribute() for namespaced attributes (MDN).
Setter counterpart: setAttributeNS() (MDN).
Foundation
📝 Syntax
General form of Element.getAttributeNS (MDN):
JavaScript
getAttributeNS(namespace, name)
Parameters
namespace — a string specifying the namespace in which to look for the attribute.
name — a string specifying the qualified name of the attribute to look for.
Return value
The string value of the specified attribute, or null if it does not exist. MDN notes that very old browsers sometimes returned an empty string instead of null—use hasAttributeNS() when that matters.
Common patterns
JavaScript
const XLINK = "http://www.w3.org/1999/xlink";
const link = document.querySelector("svg a");
const href = link.getAttributeNS(XLINK, "xlink:href");
if (href) {
console.log(href);
}
// Safer existence check (MDN):
if (link.hasAttributeNS(XLINK, "xlink:href")) {
console.log(link.getAttributeNS(XLINK, "xlink:href"));
}
Cheat Sheet
⚡ Quick Reference
Goal
Code
Namespaced string value
el.getAttributeNS(ns, "xlink:href")
Namespaced Attr node
el.getAttributeNodeNS(ns, "xlink:href")
Non-namespaced HTML attr
el.getAttribute("id")
Not found
null
MDN status
Baseline Widely available (since July 2015)
Snapshot
🔍 At a Glance
Four facts to remember about Element.getAttributeNS().
MDN: getAttributeNS() lets you target attributes in a particular namespace, not just by prefixed name alone.
Example 4 — SVG in HTML: getAttribute() Fallback
When SVG is embedded in HTML, MDN notes you may read prefixed attrs with getAttribute("test:foo").
JavaScript
const TEST_NS = "http://www.example.com/2014/test";
const circle = document.getElementById("target");
const viaNS = circle.getAttributeNS(TEST_NS, "foo");
const viaName = circle.getAttribute("test:foo");
console.log(viaNS);
console.log(viaName);
// Both return the same value in HTML documents
Element.getAttributeNS() 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.getAttributeNS()
Read namespaced attribute values as strings in SVG and XML — use getAttribute() for plain HTML attributes.
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
getAttributeNS()Excellent
Bottom line: Use getAttributeNS() for namespaced value reads in SVG/XML. Pass the correct namespace URI and qualified name; prefer getAttribute() for ordinary HTML attributes.
Wrap Up
Conclusion
Element.getAttributeNS() is the namespaced version of getAttribute(). Pass a namespace URI and qualified name to read a string value from SVG or XML documents.
Use the correct namespace URI (not just the prefix)
Null-check before using the returned string
Prefer getAttribute() for plain HTML attributes
Use hasAttributeNS() when existence is uncertain (MDN)
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 an Attr object
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.getAttributeNS()
Namespaced string reads for SVG and XML.
5
Core concepts
📝01
Returns
string | null
API
📄02
Args
URI + name
Two
✍️03
SVG/XML
namespaced
Scope
✅04
Baseline
widely available
Status
⚡05
Values
direct string
Simple
❓ Frequently Asked Questions
It returns the string value of a namespaced attribute on an element. You pass a namespace URI and the qualified attribute name (for example xlink:href). If the attribute does not exist, it returns null.
No. MDN marks Element.getAttributeNS() 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 name (the qualified name, often prefix:localName like xlink:href).
getAttribute() reads non-namespaced HTML attributes by name only. getAttributeNS() also requires the namespace URI, which is needed for SVG and XML namespaced attributes.
getAttributeNS() returns the value as a string (or null). getAttributeNodeNS() returns the full Attr object when you need metadata like namespaceURI or ownerElement.
The method returns null when nothing matches. MDN recommends using hasAttributeNS() first if you need to distinguish missing attributes from empty values on older browsers.
Did you know?
MDN notes that getAttributeNS() differs from getAttribute() because it lets you specify the attribute as part of a particular namespace. For everyday HTML id and class attributes, the non-NS method is the right tool.