JavaScript NamedNodeMap setNamedItemNS() Method

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

What You’ll Learn

The setNamedItemNS() method of a NamedNodeMap inserts or replaces an Attr node—including namespaced attributes from XML. Learn the MDN ob:one move example, MDN’s note that it is an alias of setNamedItem(), return values, and InUseAttributeError—with five examples and try-it labs.

01

Kind

Instance method

02

Returns

Old Attr|null

03

Arg

Attr node

04

Alias

setNamedItem

05

Use case

XML / SVG

06

Status

Baseline widely

Introduction

removeNamedItemNS() detaches a namespaced attribute. setNamedItemNS() attaches an Attr to a new element’s attributes map.

MDN’s example parses XML where ob:one lives in a namespace, removes it with removeNamedItemNS(uri, "one"), then calls setNamedItemNS(one) on an HTML <span>—showing prefix, localName, and qualified name in a loop.

💡
Beginner tip

MDN notes: setNamedItemNS(attr) is an alias of setNamedItem(attr)—same behavior, interchangeable. The NS name pairs with getNamedItemNS and removeNamedItemNS.

Understanding the setNamedItemNS() Method

An instance method on NamedNodeMap that puts an Attr into the map, replacing any same-name attribute.

  • attr — the Attr to insert (often detached via removeNamedItemNS).
  • Returns — replaced Attr or null if the name is new.
  • Alias — MDN: interchangeable with setNamedItem(attr).
  • Exception — thrown if the Attr is still part of another map.
  • Namespaced attrs — expose prefix, localName, namespaceURI.
  • Baseline Widely available on MDN (since July 2015).

📝 Syntax

Call the method on a NamedNodeMap (usually from element.attributes):

JavaScript
namedNodeMap.setNamedItemNS(attr)
// MDN: alias of namedNodeMap.setNamedItem(attr)

Parameters

  • attr — an Attr node to add or replace in the map.

Return value

The old Attr if replaced, or null when the name is new.

Exceptions

Thrown if the attribute is still part of another map.

Typical pattern (MDN idea)

JavaScript
const doc = new DOMParser().parseFromString(
  '<warning ob:one="test" xmlns:ob="http://www.example.com/ob">Beware!</warning>',
  "application/xml",
);
const warning = doc.querySelector("warning");
const span = document.querySelector("span");

const one = warning.attributes.removeNamedItemNS(
  "http://www.example.com/ob",
  "one",
);
span.attributes.setNamedItemNS(one);

⚡ Quick Reference

GoalCode / note
Insert Attrel.attributes.setNamedItemNS(attr)
Move namespaced attrremoveNamedItemNS then setNamedItemNS
Same assetNamedItem(attr) (MDN alias)
New nameReturns null
Still on other elementThrows (MDN)
MDN statusBaseline Widely available (since July 2015)

🔍 At a Glance

Four facts to remember about NamedNodeMap.setNamedItemNS().

Returns
Attr|null

Old or new

Arg
Attr

Node object

Alias
setNamedItem

MDN note

Baseline
widely

Since Jul 2015

Examples Gallery

Examples follow MDN NamedNodeMap.setNamedItemNS. Labs use DOMParser and namespaced XML attributes.

📚 Getting Started

MDN pattern: move a namespaced attribute onto an HTML element.

Example 1 — MDN Move Namespaced ob:one to <span>

Remove from parsed XML, set on an HTML element’s attributes map.

JavaScript
const doc = new DOMParser().parseFromString(
  '<warning ob:one="test" xmlns:ob="http://www.example.com/ob">Beware!</warning>',
  "application/xml",
);
const warning = doc.querySelector("warning");
const span = document.querySelector("span");
const attrMap = span.attributes;

const one = warning.attributes.removeNamedItemNS(
  "http://www.example.com/ob",
  "one",
);
attrMap.setNamedItemNS(one);

console.log(span.attributes.length, one.value);
Try It Yourself

How It Works

MDN: detach with removeNamedItemNS, then setNamedItemNS on the target map.

Example 2 — List prefix, localName, and name

MDN loops for (const attr of attrMap) after insertion.

JavaScript
const doc = new DOMParser().parseFromString(
  '<warning ob:one="test" xmlns:ob="http://www.example.com/ob"/>',
  "application/xml",
);
const span = document.querySelector("span");
const one = doc.documentElement.attributes.removeNamedItemNS(
  "http://www.example.com/ob",
  "one",
);
span.attributes.setNamedItemNS(one);

const lines = [];
for (const attr of span.attributes) {
  lines.push(`${attr.prefix}|${attr.localName}|${attr.name}`);
}
console.log(lines.join("\n"));
Try It Yourself

How It Works

Namespaced Attr nodes expose prefix and localName metadata after insertion.

📈 Edge Cases & Comparisons

Return values, alias behavior, and in-use errors.

Example 3 — New Attribute Returns null

Create a namespaced Attr with createAttributeNS.

JavaScript
const box = document.querySelector("#demo");
const uri = "https://example.com/app";
const attr = document.createAttributeNS(uri, "app:id");
attr.value = "42";

const replaced = box.attributes.setNamedItemNS(attr);

console.log(replaced === null, box.getAttributeNS(uri, "id"));
Try It Yourself

How It Works

MDN: return null when the attribute name is new to the map.

Example 4 — setNamedItemNS() vs setNamedItem() (MDN Alias)

MDN: both methods are interchangeable.

JavaScript
const a = document.querySelector("#box-a");
const b = document.querySelector("#box-b");

const attrA = document.createAttribute("data-x");
attrA.value = "1";
const viaNS = a.attributes.setNamedItemNS(attrA);

const attrB = document.createAttribute("data-y");
attrB.value = "2";
const viaNamed = b.attributes.setNamedItem(attrB);

console.log(viaNS === null, viaNamed === null);
Try It Yourself

How It Works

Same signature, same return semantics—MDN lists them as aliases.

Example 5 — InUseAttributeError When Attr Is Still Attached

Cannot set an Attr that still belongs to another element’s map.

JavaScript
const doc = new DOMParser().parseFromString(
  '<warning ob:one="test" xmlns:ob="http://www.example.com/ob"/>',
  "application/xml",
);
const warning = doc.documentElement;
const target = document.querySelector("#demo");

const stillAttached = warning.attributes.getNamedItemNS(
  "http://www.example.com/ob",
  "one",
);

try {
  target.attributes.setNamedItemNS(stillAttached);
} catch (error) {
  console.log(error.name);
}
Try It Yourself

How It Works

Remove with removeNamedItemNS first, then call setNamedItemNS.

🚀 Common Use Cases

  • Move namespaced XML attributes between elements (MDN demo).
  • Insert Attr nodes from createAttributeNS().
  • Teach namespace-aware DOM alongside getNamedItemNS / removeNamedItemNS.
  • Interop with legacy code using the NS-named NamedNodeMap API.
  • Inspect prefix and localName after insertion.

🔧 How It Works

1

Get or detach Attr

From createAttributeNS, or removeNamedItemNS on the source element (MDN).

Input
2

Call setNamedItemNS(attr)

Alias of setNamedItem—Attr must not still be on another element.

Insert
3

Return old or null

Replaced same-name attr → old Attr. New name → null.

Result
4

Element reflects change

getNamedItemNS(uri, local) finds the attr; prefix/localName available on the Attr node.

📝 Notes

  • MDN: Baseline Widely available (since July 2015) — no Deprecated / Experimental / Non-standard banner.
  • MDN: alias of setNamedItem(attr)—interchangeable.
  • Detach with removeNamedItemNS before moving namespaced attrs.
  • Prefer setAttributeNS() for simple string updates.
  • Related learning: setNamedItem(), removeNamedItemNS(), setAttributeNodeNS(), Element.attributes.

Universal Browser Support

NamedNodeMap.setNamedItemNS() is marked Baseline Widely available on MDN (since July 2015). Logos use the shared browser-image-sprite.png sprite from this project.

Baseline · Widely available

NamedNodeMap.setNamedItemNS()

Inserts or replaces an Attr—alias of setNamedItem, returns old Attr or null, throws if still in another map.

Universal Widely available
Google Chrome Full support · Desktop & Mobile
Full support
Mozilla Firefox Full support · Desktop & Mobile
Full support
Apple Safari Full support · macOS & iOS
Full support
Microsoft Edge Full support · Chromium
Full support
Opera Full support · Modern versions
Full support
Internet Explorer Supported on NamedNodeMap (legacy DOM)
Legacy OK
NamedNodeMap.setNamedItemNS() Excellent

Bottom line: Call map.setNamedItemNS(attr) after removeNamedItemNS to move namespaced attributes between elements.

Conclusion

NamedNodeMap.setNamedItemNS() inserts or replaces an Attr, returning the old node or null. MDN lists it as an alias of setNamedItem(). Use it with removeNamedItemNS() to move namespaced attributes, and prefer setAttributeNS() when strings are enough.

Continue with length, setNamedItem(), removeNamedItemNS(), or the JavaScript hub.

💡 Best Practices

✅ Do

  • Detach with removeNamedItemNS before moving attrs
  • Use createAttributeNS for new namespaced Attr nodes
  • Remember MDN: interchangeable with setNamedItem
  • Check return value for replaced vs new attributes
  • Use setAttributeNS for simple string updates

❌ Don’t

  • Set an Attr still attached to another element
  • Assume NS and non-NS methods differ in behavior (MDN alias)
  • Use this for ordinary HTML when setAttribute suffices
  • Forget null means a brand-new attribute name
  • Skip try/catch when reusing uncertain Attr references

Key Takeaways

Knowledge Unlocked

Five things to remember about setNamedItemNS()

Namespaced Attr insertion—alias of setNamedItem.

5
Core concepts
⚙️02

Arg

Attr

Params
⚠️03

Alias

setNamedItem

MDN
📄04

Move

remove then set

Pattern
🎯05

Baseline

since Jul 2015

Status

❓ Frequently Asked Questions

It inserts an Attr node into the map by its name. If a same-name attribute already exists, it is replaced. MDN notes it is an alias of setNamedItem()—you can use them interchangeably.
No. MDN marks NamedNodeMap.setNamedItemNS() as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard.
MDN states they are aliases with the same behavior. The NS name exists for symmetry with other namespace-aware NamedNodeMap methods like getNamedItemNS and removeNamedItemNS.
The old Attr node if an attribute with the same name was replaced, or null if the attribute is new to the map.
MDN documents an exception when the Attr is still part of another map—typically InUseAttributeError if the attribute is still attached to a different element.
When moving or inserting namespaced Attr nodes from XML/SVG—often after removeNamedItemNS(). For everyday HTML strings, setAttribute() or setAttributeNS() is usually simpler.
Did you know?

MDN explicitly notes that setNamedItemNS(attr) is an alias of setNamedItem(attr)—the NS name exists for symmetry with getNamedItemNS and removeNamedItemNS, not different runtime behavior.

Explore length

Count attributes on element.attributes with the read-only length property.

length property →

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.

5 people found this page helpful