Element.setAttributeNode() is an instance method that adds an Attr node to an element. Learn MDN’s clone- lang example, document.createAttribute(), the replaced-node return value, when to prefer setAttribute(), and five try-it labs aligned with MDN.
01
Kind
Instance method
02
Arg
Attr node
03
Returns
Replaced Attr
04
Clone
cloneNode(true)
05
vs setAttribute
node vs string
06
Status
Baseline widely
Fundamentals
Introduction
Day-to-day code usually writes attributes with element.setAttribute("lang", "en-US"). setAttributeNode() works at the Attr node level: you pass an actual Attr object instead of a name/value pair.
MDN recommends this API when you already have (or need to build) an attribute node—for example cloning lang from one element onto another. If you only need to set a string value, prefer setAttribute().
💡
Beginner tip
An Attr can belong to only one element. To copy an attribute, clone it first: attr.cloneNode(true), then call target.setAttributeNode(clone) (MDN).
Calling el.setAttributeNode(attr) attaches the given Attr to el. If an attribute with the same name already exists, that attribute is replaced and the old node is returned (MDN).
It is an instance method on Element.
Takes one parameter: an Attr node to add (MDN).
Returns the replaced Attr node, if any (MDN).
Prefer setAttribute() when you do not need to work with Attr nodes (MDN).
Clone before reusing an Attr from another element.
Pairs with getAttributeNode() and removeAttributeNode().
Foundation
📝 Syntax
General form of Element.setAttributeNode (MDN):
JavaScript
setAttributeNode(attribute)
Parameters
Parameter
Type
Description
attribute
Attr
The Attr node to add to the element (MDN).
Return value
Type
Description
Attr or empty
The replaced attribute node, if any (MDN).
Notes
MDN: if the named attribute already exists on the element, that attribute is replaced with the new one and the replaced one is returned.
Common patterns
JavaScript
// MDN — clone lang from one element to another
const d1 = document.getElementById("one");
const d2 = document.getElementById("two");
const a = d1.getAttributeNode("lang");
d2.setAttributeNode(a.cloneNode(true));
// Create a new Attr from scratch
const attr = document.createAttribute("data-role");
attr.value = "admin";
el.setAttributeNode(attr);
// Capture the replaced node (if any)
const old = el.setAttributeNode(document.createAttribute("title"));
// old is the previous title Attr, or empty if none existed
Cheat Sheet
⚡ Quick Reference
Goal
Code
Add an Attr node
el.setAttributeNode(attr)
Clone Attr to another element
t.setAttributeNode(a.cloneNode(true))
Create Attr then set
document.createAttribute("id")
Simple name/value write
el.setAttribute("id", "x")
Get Attr first
el.getAttributeNode("lang")
MDN status
Baseline Widely available (since July 2015)
Snapshot
🔍 At a Glance
Four facts to remember about Element.setAttributeNode().
getAttributeNode("lang") returns the Attr. cloneNode(true) makes a copy so both elements can have their own Attr. Then setAttributeNode attaches the clone to d2 (MDN).
Example 2 — Create Attr with createAttribute
Build a new attribute node from scratch and attach it.
Element.setAttributeNode() 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.setAttributeNode()
Add or replace Attr nodes on an element — the Attr-level companion to setAttribute().
BaselineWidely available
Google ChromeSupported · Desktop & Mobile
Yes
Microsoft EdgeSupported · Chromium
Yes
Mozilla FirefoxSupported · Desktop & Mobile
Yes
Apple SafariSupported · macOS & iOS
Yes
OperaSupported · Modern versions
Yes
Internet ExplorerSupported in legacy IE
Yes
setAttributeNode()Excellent
Bottom line: Use setAttributeNode() when you work with Attr objects (clone, createAttribute, capture replaced nodes). Prefer setAttribute() for everyday name/value writes.
Wrap Up
Conclusion
Element.setAttributeNode() attaches an Attr node to an element and returns any replaced Attr. It shines for clone and Attr-object workflows; for simple string sets, use setAttribute().
Clone Attrs with cloneNode(true) when copying (MDN)
Use setAttribute() for simple name/value writes (MDN)
Capture the return value when replacing an existing Attr
Pair with getAttributeNode() for read/write Attr flows
Set attr.value before attaching a new Attr
❌ Don’t
Reuse the same Attr on two elements without cloning
Prefer Attr APIs when a string set is enough
Forget that same-name attrs replace the old node
Confuse with setAttributeNS / setAttributeNodeNS
Ignore ownerElement when debugging Attr ownership
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about Element.setAttributeNode()
Attach Attr nodes — clone when copying.
5
Core concepts
📝01
Arg
Attr node
API
📄02
Returns
replaced Attr
Value
📋03
Clone
cloneNode(true)
Copy
✅04
Baseline
widely available
Status
⚡05
Prefer
setAttribute
Simple
❓ Frequently Asked Questions
It adds an Attr node to the element. If an attribute with the same name already exists, that attribute is replaced and the old Attr is returned (MDN).
No. MDN marks Element.setAttributeNode() as Baseline Widely available (across browsers since July 2015). It is not Deprecated, Experimental, or Non-standard.
The replaced Attr node if one existed with the same name; otherwise typically null/undefined for a new attribute (MDN: the replaced attribute node, if any).
MDN: use setAttributeNode() when you need to work with the Attr node first (for example cloning from another element). Prefer setAttribute() for simple name/value writes.
An Attr can belong to only one element. Clone it with attr.cloneNode(true) before calling setAttributeNode on the target (MDN example).
Use document.createAttribute(name), set attr.value, then element.setAttributeNode(attr).
Did you know?
MDN’s classic demo clones lang from one element to another with getAttributeNode + cloneNode(true) + setAttributeNode—the same pattern works for any attribute you need to copy.