Element.setAttributeNodeNS() adds a namespaced Attr node to an element (MDN). Prefer setAttributeNS() for simple namespace + name + value writes, and setAttribute() when no namespace is needed (typical HTML).
Pass the required arguments shown below. Check return values and browser support before you rely on the result.
Syntax
Call el.setAttributeNodeNS. Start with this line.
JavaScript
el.setAttributeNodeNS(attr)
Call
What it does
el.setAttributeNodeNS(attr)
Primary signature.
el.setAttributeNodeNS(...)
Call with the arguments your case needs.
Built into the browser. Call setAttributeNodeNS(...) on an Element — no import required.
Library declarations
JavaScript
el.setAttributeNodeNS(attr)
Parameters
Parameter
Description
attributeNode
Required. Namespaced Attr to add
Clone
Optional. Required — Attr cannot be in use on two elements
Return Value
Return Value
Description
*
Replaced Attr, or null if none
JavaScript Examples
Example 1 — Clone a Namespaced Attr
MDN: get with getAttributeNodeNS, clone, then setAttributeNodeNS.
Return. Old Attr comes back from setAttributeNodeNS.
Element. Now holds the new Attr’s value.
Compatibility
Browser Support
Element.setAttributeNodeNS() is Baseline Widely available (MDN, since July 2015). It is not Deprecated, Experimental, or Non-standard.
✓ Baseline Widely available
Element.setAttributeNodeNS()
Add a namespaced Attr node. Prefer setAttributeNS() unless you need the Attr object (clone, inspect, replace).
BaselineWidely available
Google ChromeSupported · Desktop & Mobile
Full support
Mozilla FirefoxSupported · Desktop & Mobile
Full support
Apple SafariSupported · macOS & iOS
Full support
Microsoft EdgeSupported · Chromium
Full support
Internet ExplorerNo native support · Use a polyfill
Polyfill
OperaSupported · Modern versions
Full support
Samsung InternetSupported · Android
Full support
Android WebViewSupported · Modern WebView
Full support
setAttributeNodeNS()Baseline
Bottom line: Fully supported. Use for cloning namespaced Attrs; otherwise setAttributeNS() is clearer.
Frequently Asked Questions
It adds a namespaced Attr node to an element. If an attribute with the same namespace and local name already exists, it is replaced and the old Attr is returned (MDN).
No. MDN marks Element.setAttributeNodeNS() as Baseline Widely available (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 the Attr node first (for example cloning). Prefer setAttributeNS() for simple namespace + name + value writes.
MDN: without cloning you may get NS_ERROR_DOM_INUSE_ATTRIBUTE_ERR (Attribute already in use). 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.