Element.setAttributeNS() is an instance method that sets a namespaced attribute by namespace URI and qualified name. Learn MDN’s spec:align example, SVG xlink:href, when to prefer setAttribute(), XSS injection-sink warnings, and five try-it labs aligned with MDN.
01
Kind
Instance method
02
Args
NS, name, value
03
Returns
undefined
04
SVG/XML
xlink, custom NS
05
Security
XSS sinks
06
Status
Baseline widely
Fundamentals
Introduction
Most HTML pages use setAttribute("class", "active") for plain attributes. SVG and XML also need namespaced attributes tied to a namespace URI—for example xlink:href on an SVG link.
element.setAttributeNS(namespaceURI, qualifiedName, value) adds or updates that namespaced attribute (MDN). For HTML attributes without a namespace, use setAttribute() instead.
💡
Security note (MDN)
Like setAttribute(), some attributes are injection sinks (e.g. onclick, srcdoc, script src). Never pass untrusted user input to those. Use safe alternatives or Trusted Types when enforced.
Element.setAttributeNS() 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.setAttributeNS()
Set or update namespaced attributes by URI and qualified name — the standard DOM API for SVG and XML.
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
setAttributeNS()Excellent
Bottom line: Use setAttributeNS() for SVG/XML namespaced writes, setAttribute() for plain HTML, and setAttributeNodeNS() when you need Attr-node workflows.
Wrap Up
Conclusion
Element.setAttributeNS() adds or updates namespaced attributes by URI and qualified name. It is Baseline widely available, returns undefined, and pairs naturally with getAttributeNS() and removeAttributeNS().
Use setAttributeNS() for SVG and XML namespaced attributes
Store namespace URIs in constants (XLINK, MY_NS)
Prefer setAttributeNS() over Attr-node APIs for simple writes (MDN)
Verify with getAttributeNS() after setting
Use setAttribute() for plain HTML attributes (MDN)
❌ Don’t
Pass untrusted strings to injection-sink attributes
Use setAttributeNS() when setAttribute() is enough
Expect a useful return value (it is undefined)
Confuse namespace URI with element namespace
Use attribute strings for event handlers — use addEventListener
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about Element.setAttributeNS()
Set namespaced attributes — SVG, XML, and custom namespaces.
5
Core concepts
📝01
Returns
undefined
API
📄02
Args
ns, name, value
Triple
🌐03
SVG
xlink:href
Common
✅04
Baseline
widely available
Status
⚡05
HTML
setAttribute()
Plain attrs
❓ Frequently Asked Questions
It adds a new attribute or changes the value of an attribute with the given namespace and qualified name (MDN).
No. MDN marks Element.setAttributeNS() as Baseline Widely available (across browsers since July 2015). It is not Deprecated, Experimental, or Non-standard.
undefined. There is no return value (MDN).
namespaceURI (string), qualifiedName (string, e.g. xlink:href), and value (string or trusted type) (MDN).
MDN: for HTML documents when you do not need to specify the attribute as part of a namespace, use setAttribute() instead.
Some attributes are XSS injection sinks (onclick, srcdoc, script src). Never pass untrusted strings to those. MDN documents the same security considerations as setAttribute().
Did you know?
MDN’s setAttributeNS() demo uses setAttributeNS("http://www.mozilla.org/ns/specialspace", "spec:align", "center")—the namespace URI and qualified name together identify the attribute, not just the local name.