The setNamedItem() method of a NamedNodeMapinserts or replaces an Attr node by name. Learn the MDN move-class example, what the return value means, the InUseAttributeError when an Attr is still attached elsewhere, and how it compares with setAttributeNode()—with five examples and try-it labs.
01
Kind
Instance method
02
Returns
Old Attr|null
03
Arg
Attr node
04
Replace
same name
05
Use case
Move attrs
06
Status
Baseline widely
Fundamentals
Introduction
getNamedItem() reads attributes; removeNamedItem() deletes them. setNamedItem() is how you add or update an Attr on element.attributes.
MDN’s classic demo removes class from a <span> and moves it onto a <pre> with pre.attributes.setNamedItem(classAttribute)—then shows what happens when you try to set an Attr that is still on another element.
💡
Beginner tip
For most HTML tasks, element.setAttribute("class", "foo") is enough. Use setNamedItem when you already have an Attr node—especially after removeNamedItem() detached it from its old owner.
Concept
Understanding the setNamedItem() Method
An instance method on NamedNodeMap that puts an Attr into the map, keyed by its name.
attr — the Attr node to insert (from createAttribute, removeNamedItem, etc.).
Returns — the replaced Attr if a same-name attribute existed, or null if the name is new.
Replace rule — same name in the map → old Attr is replaced.
Exception — thrown if the Attr is still part of another map (MDN).
Common source — element.attributes.setNamedItem(attr).
Baseline Widely available on MDN (since July 2015).
Foundation
📝 Syntax
Call the method on a NamedNodeMap (usually from element.attributes):
JavaScript
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 (e.g. still on a different element).
Typical pattern (MDN idea)
JavaScript
const span = document.querySelector("span");
const pre = document.querySelector("pre");
const classAttribute = span.attributes.removeNamedItem("class");
pre.attributes.setNamedItem(classAttribute);
console.log(pre.className); // "foo" — class moved from span to pre
Remove from one map, set on another—attribute travels with the Attr node.
JavaScript
const span = document.querySelector("span");
const pre = document.querySelector("pre");
let result = `The <pre> element initially contains ${pre.attributes.length} attributes.\n\n`;
result += "We remove `class` from <span> and add it to <pre>.\n";
const classAttribute = span.attributes.removeNamedItem("class");
pre.attributes.setNamedItem(classAttribute);
result += `The <pre> element now contains ${pre.attributes.length} attributes.`;
console.log(result);
NamedNodeMap.setNamedItem() 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.setNamedItem()
Inserts or replaces an Attr by name—returns the old Attr or null, throws if the Attr is 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.setNamedItem()Excellent
Bottom line: Call map.setNamedItem(attr) on element.attributes to add or replace attributes with Attr nodes.
Wrap Up
Conclusion
NamedNodeMap.setNamedItem() inserts or replaces an Attr by name, returning the old node or null. Use it to move attributes between elements (MDN), detach with removeNamedItem first, and prefer setAttribute() for everyday HTML.
Detach with removeNamedItem before moving Attr nodes
Use createAttribute for brand-new free Attr objects
Check the return value to see if you replaced an old Attr
Use setAttribute for simple name/value updates
Wrap setNamedItem in try/catch when reuse is uncertain
❌ Don’t
Set an Attr still attached to another element (MDN throws)
Assume it always returns null
Use setNamedItem when strings suffice
Forget that same-name attrs are silently replaced
Move attrs without updating your mental model of owner elements
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about setNamedItem()
Insert or replace Attr nodes by name.
5
Core concepts
🔗01
Returns
old|null
API
⚙️02
Arg
Attr
Params
⚠️03
In use
throws
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 an attribute with the same name already exists, it is replaced. On elements, element.attributes.setNamedItem(attr) adds or updates that attribute on the element.
No. MDN marks NamedNodeMap.setNamedItem() as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard.
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 you try to set an attribute that is still attached to a different element.
setNamedItem() takes an Attr node on the NamedNodeMap. element.setAttribute(name, value) takes strings and creates or updates the attribute for you.
When moving Attr nodes between elements (MDN example), or when you already have an Attr object from removeNamedItem() or createAttribute(). For everyday HTML, setAttribute() is usually simpler.
Did you know?
MDN’s setNamedItem() demo moves class from <span> to <pre> with removeNamedItem + setNamedItem, then shows InUseAttributeError when trying to set id while it is still on the span.