JavaScript Element setAttributeNodeNS() Method

Beginner
⏱️ 11 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Baseline
Instance method

What You’ll Learn

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

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”).

This page is part of JavaScript Element. Pair with getAttributeNodeNS(), getAttributeNS(), and removeAttributeNS().

Understanding the setAttributeNodeNS() Method

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).

📝 Syntax

General form of Element.setAttributeNodeNS (MDN):

JavaScript
setAttributeNodeNS(attributeNode)

Parameters

ParameterTypeDescription
attributeNodeAttrThe namespaced Attr node to add to the element (MDN).

Return value

TypeDescription
Attr or emptyThe 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.

Common patterns

JavaScript
const MY_NS = "http://www.mozilla.org/ns/specialspace";
const d1 = document.getElementById("one");
const d2 = document.getElementById("two");

// MDN — clone namespaced Attr
const a = d1.getAttributeNodeNS(MY_NS, "special-align");
d2.setAttributeNodeNS(a.cloneNode(true));

// Create namespaced Attr from scratch
const attr = document.createAttributeNS(MY_NS, "special-align");
attr.value = "utterleft";
el.setAttributeNodeNS(attr);

// Simple write? Prefer setAttributeNS (MDN)
el.setAttributeNS(MY_NS, "special-align", "utterleft");

⚡ Quick Reference

GoalCode
Add namespaced Attrel.setAttributeNodeNS(attr)
Clone from another elementt.setAttributeNodeNS(a.cloneNode(true))
Create namespaced Attrdocument.createAttributeNS(ns, name)
Simple NS writeel.setAttributeNS(ns, name, val)
Get Attr firstel.getAttributeNodeNS(ns, name)
MDN statusBaseline Widely available (since July 2015)

🔍 At a Glance

Four facts to remember about Element.setAttributeNodeNS().

Arg
Attr

Namespaced node

Returns
old Attr?

If replaced

Clone
required

MDN rule

Prefer
setAttrNS

Simple writes

📋 setAttributeNodeNS() vs setAttributeNS()

setAttributeNodeNS(attr)setAttributeNS(ns, name, val)setAttributeNode(attr)
NamespaceOn the Attr nodeExplicit URI paramNon-namespaced
InputAttr objectURI + name + valueAttr object
ReturnsReplaced Attr (if any)undefinedReplaced Attr (if any)
Best forClone / Attr workflowsEveryday NS writesHTML Attr workflows
MDN guidanceWhen you need the nodePreferred for simple setsNon-NS HTML attrs

Examples Gallery

Examples follow MDN Element.setAttributeNodeNS() patterns. Use View Output or Try It Yourself for each case.

📚 Getting Started

MDN’s namespaced demo—clone special-align and create with createAttributeNS.

Example 1 — Clone Namespaced Attr (MDN)

Copy special-align from one element to another with getAttributeNodeNS + clone.

JavaScript
const MY_NS = "http://www.mozilla.org/ns/specialspace";
// <div id="one" xmlns:myNS="..." myNS:special-align="utterleft">
// <div id="two">
const d1 = document.getElementById("one");
const d2 = document.getElementById("two");
const a = d1.getAttributeNodeNS(MY_NS, "special-align");

d2.setAttributeNodeNS(a.cloneNode(true));

console.log(d2.getAttributeNS(MY_NS, "special-align")); // "utterleft"
Try It Yourself

How It Works

MDN: get the namespaced Attr, clone it, then call setAttributeNodeNS on the target element.

Example 2 — Create with createAttributeNS

Build a namespaced Attr node and attach it with setAttributeNodeNS.

JavaScript
const MY_NS = "http://www.mozilla.org/ns/specialspace";
const el = document.getElementById("box");
const attr = document.createAttributeNS(MY_NS, "special:specialAlign");
attr.value = "center";

el.setAttributeNodeNS(attr);

console.log(el.getAttributeNS(MY_NS, "specialAlign")); // "center"
Try It Yourself

How It Works

createAttributeNS builds the namespaced Attr; setAttributeNodeNS attaches it to the element.

📈 Practical Patterns

Replacement return value, clone requirement, and API choice.

Example 3 — Replaced Namespaced Attr (MDN)

When the namespaced attribute already exists, the old Attr is returned.

JavaScript
const MY_NS = "http://www.mozilla.org/ns/specialspace";
const el = document.getElementById("item");
// has special:specialAlign="old"

const next = document.createAttributeNS(MY_NS, "special:specialAlign");
next.value = "new";

const replaced = el.setAttributeNodeNS(next);

console.log(replaced.value);                        // "old"
console.log(el.getAttributeNS(MY_NS, "specialAlign")); // "new"
Try It Yourself

How It Works

MDN: the existing namespaced attribute is replaced and the replaced node is returned.

Example 4 — Clone Avoids INUSE Error (MDN)

Reusing an Attr without cloning can throw “Attribute already in use”.

JavaScript
const MY_NS = "http://www.mozilla.org/ns/specialspace";
const d1 = document.getElementById("one");
const d2 = document.getElementById("two");
const a = d1.getAttributeNodeNS(MY_NS, "special-align");

// Safe — clone first (MDN)
d2.setAttributeNodeNS(a.cloneNode(true));

// Risky — may throw INUSE_ATTRIBUTE_ERR:
// d2.setAttributeNodeNS(a);
Try It Yourself

How It Works

MDN: unlike other nodes, Attr must be cloned to be reused on another element.

Example 5 — Prefer setAttributeNS() for Simple Writes

MDN: use setAttributeNS() when you do not need the Attr node first.

JavaScript
const MY_NS = "http://www.mozilla.org/ns/specialspace";
const el = document.getElementById("box");

// Preferred for simple cases (MDN)
el.setAttributeNS(MY_NS, "special:specialAlign", "utterleft");

// Attr-node workflow (more steps)
const attr = document.createAttributeNS(MY_NS, "special:specialAlign");
attr.value = "utterleft";
el.setAttributeNodeNS(attr);

console.log(el.getAttributeNS(MY_NS, "specialAlign")); // "utterleft"
Try It Yourself

How It Works

Both set the same namespaced value. Use setAttributeNodeNS() when you already have an Attr to clone or replace.

🚀 Common Use Cases

  • Cloning namespaced attributes such as special-align between elements (MDN).
  • Building namespaced Attr nodes with document.createAttributeNS().
  • Capturing the previous Attr when replacing a namespaced attribute.
  • Working with Attr nodes from getAttributeNodeNS().
  • SVG/XML workflows where namespace URIs matter.
  • Pairing with removeAttributeNS() for full namespaced attribute lifecycle.

🧠 How setAttributeNodeNS() Attaches Namespaced Attr Nodes

1

Obtain namespaced Attr

Create with createAttributeNS, or get/clone with getAttributeNodeNS.

Attr
2

Call setAttributeNodeNS

el.setAttributeNodeNS(attr) adds the namespaced node (MDN).

Attach
3

Replace if same NS + name

Existing namespaced attribute is replaced; old Attr returned (MDN).

Replace
4

Namespaced markup updated

Verify with getAttributeNS() or hasAttributeNS().

📝 Notes

  • Not Deprecated, Experimental, or Non-standard on MDN (Baseline Widely available, July 2015).
  • 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).
  • Without cloning you may get INUSE_ATTRIBUTE_ERR (MDN).
  • Related: getAttributeNodeNS(), getAttributeNS(), removeAttributeNS(), JavaScript hub.

Browser Support

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().

Baseline Widely available
Google Chrome Supported · Desktop & Mobile
Yes
Microsoft Edge Supported · Chromium
Yes
Mozilla Firefox Supported · Desktop & Mobile
Yes
Apple Safari Supported · macOS & iOS
Yes
Opera Supported · Modern versions
Yes
Internet Explorer Supported 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.

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().

Continue with getAttributeNodeNS(), setAttributeNS(), removeAttributeNS(), shadowRoot, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Clone namespaced Attrs with cloneNode(true) (MDN)
  • 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

Key Takeaways

Knowledge Unlocked

Five things to remember about Element.setAttributeNodeNS()

Namespaced Attr nodes — clone when copying.

5
Core concepts
📄 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.

Next: setAttributeNS()

Continue with simple namespaced attribute writes.

setAttributeNS() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful