The setNamedItemNS() method of a NamedNodeMapinserts 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
Fundamentals
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.
Concept
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.
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.
UniversalWidely available
Google ChromeFull support · Desktop & Mobile
Full support
Mozilla FirefoxFull support · Desktop & Mobile
Full support
Apple SafariFull support · macOS & iOS
Full support
Microsoft EdgeFull support · Chromium
Full support
OperaFull support · Modern versions
Full support
Internet ExplorerSupported on NamedNodeMap (legacy DOM)
Legacy OK
NamedNodeMap.setNamedItemNS()Excellent
Bottom line: Call map.setNamedItemNS(attr) after removeNamedItemNS to move namespaced attributes between elements.
Wrap Up
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.
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
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about setNamedItemNS()
Namespaced Attr insertion—alias of setNamedItem.
5
Core concepts
🔗01
Returns
old|null
API
⚙️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.