Element.setAttributeNodeNS() is an instance method that adds a namespaced Attr node to an element. Learn MDN’s special-align clone demo, createAttributeNS, the INUSE_ATTRIBUTE_ERR rule, when to prefer setAttributeNS(), and five try-it labs aligned with MDN.
01
Kind
Instance method
02
Arg
Attr node
03
Returns
Replaced Attr
04
Clone
Required (MDN)
05
vs setAttrNS
node vs string
06
Status
Baseline widely
Fundamentals
Introduction
SVG and XML documents often use namespaced attributes—for example xlink:href or custom attributes tied to a namespace URI. To attach a namespaced attribute as an Attr object, call element.setAttributeNodeNS(attr).
MDN: if you do not need to work with the Attr node first (such as cloning from another element), use setAttributeNS() instead. For plain HTML attributes without a namespace, use setAttribute().
💡
Clone required (MDN)
Unlike other DOM nodes, an Attr cannot simply move between elements. Clone it with attr.cloneNode(true) before setAttributeNodeNS, or you may get NS_ERROR_DOM_INUSE_ATTRIBUTE_ERR (“Attribute already in use”).
Calling el.setAttributeNodeNS(attr) attaches a namespaced Attr to el. If a namespaced attribute with the same identity already exists, it is replaced and the old node is returned (MDN).
It is an instance method on Element.
Takes one parameter: a namespaced Attr node (MDN).
Returns the replaced Attr node, if any (MDN).
Prefer setAttributeNS() for simple namespace + name + value writes (MDN).
Prefer setAttribute() for non-namespaced HTML attributes (MDN).
Always clone Attr nodes before attaching to another element (MDN).
Foundation
📝 Syntax
General form of Element.setAttributeNodeNS (MDN):
JavaScript
setAttributeNodeNS(attributeNode)
Parameters
Parameter
Type
Description
attributeNode
Attr
The namespaced Attr node to add to the element (MDN).
Return value
Type
Description
Attr or empty
The replaced attribute node, if any (MDN).
Notes
MDN: if the named attribute already exists, it is replaced and the replaced node is returned. Without cloning, reusing an Attr may throw NS_ERROR_DOM_INUSE_ATTRIBUTE_ERR.
Element.setAttributeNodeNS() 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.setAttributeNodeNS()
Add or replace namespaced Attr nodes — the Attr-level companion to setAttributeNS().
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
setAttributeNodeNS()Excellent
Bottom line: Use setAttributeNodeNS() when you work with namespaced Attr objects (clone, createAttributeNS, capture replaced nodes). Prefer setAttributeNS() for everyday writes.
Wrap Up
Conclusion
Element.setAttributeNodeNS() attaches a namespaced Attr node and returns any replaced Attr. Use it for clone and Attr-object workflows; for simple writes, prefer setAttributeNS() or setAttribute().
Use setAttributeNS() for simple namespaced writes (MDN)
Capture the return value when replacing an existing Attr
Pair with getAttributeNodeNS() for read/write flows
Use createAttributeNS(ns, name) for new namespaced Attrs
❌ Don’t
Reuse an Attr on two elements without cloning
Use NS APIs for plain HTML attrs when unnecessary (MDN)
Forget namespace URI when reading back with getAttributeNS
Confuse setAttributeNode with setAttributeNodeNS
Ignore INUSE_ATTRIBUTE_ERR when moving Attrs
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about Element.setAttributeNodeNS()
Namespaced Attr nodes — clone when copying.
5
Core concepts
📝01
Arg
Attr node
API
📄02
Returns
replaced Attr
Value
📋03
Clone
required
MDN
✅04
Baseline
widely available
Status
⚡05
Prefer
setAttrNS
Simple
❓ Frequently Asked Questions
It adds a namespaced Attr node to an element. If an attribute with the same namespace and name already exists, it is replaced and the old Attr is returned (MDN).
No. MDN marks Element.setAttributeNodeNS() as Baseline Widely available (across browsers since July 2015). It is not Deprecated, Experimental, or Non-standard.
The replaced namespaced Attr node, if any (MDN).
MDN: use setAttributeNodeNS() when you need to work with the Attr node first (for example cloning from another element). Prefer setAttributeNS() for simple namespace + name + value writes.
MDN: without cloning you may get NS_ERROR_DOM_INUSE_ATTRIBUTE_ERR (Attribute already in use). Unlike other nodes, Attr must be cloned to be reused on another element.
MDN: for HTML documents when you do not need a specific namespace, use setAttribute() instead of the NS variants.
Did you know?
MDN’s namespaced demo clones special-align with getAttributeNodeNS + cloneNode(true) + setAttributeNodeNS—always clone before attaching an Attr to another element.