Element.setAttributeNode() adds a new Attr node to the element (MDN). Prefer setAttribute() for simple name/value writes. Use setAttributeNode() when you already have an Attr (for example after cloning from another element).
Pass the required arguments shown below. Check return values and browser support before you rely on the result.
Syntax
Call el.setAttributeNode. Start with this line.
JavaScript
el.setAttributeNode(attr)
Call
What it does
el.setAttributeNode(attr)
Primary signature.
el.setAttributeNode(...)
Call with the arguments your case needs.
Built into the browser. Call setAttributeNode(...) on an Element — no import required.
Library declarations
JavaScript
el.setAttributeNode(attr)
Parameters
Parameter
Description
attribute
Required. The Attr node to add
Clone
Optional. Attr cannot sit on two elements at once
Namespaces
Optional. Use setAttributeNodeNS() for NS Attrs
Return Value
Return Value
Description
*
Replaced Attr, or null if none
JavaScript Examples
Example 1 — Clone an Attribute
MDN: copy lang from one element to another via Attr + clone.
Return. Old Attr comes back from setAttributeNode.
Element. Now holds the new Attr’s value.
Compatibility
Browser Support
Element.setAttributeNode() is Baseline Widely available (MDN, since July 2015). It is not Deprecated, Experimental, or Non-standard.
✓ Baseline Widely available
Element.setAttributeNode()
Add an Attr node to an element. Prefer setAttribute() unless you need the Attr object (clone, inspect, replace).
BaselineWidely available
Google ChromeSupported · Desktop & Mobile
Full support
Mozilla FirefoxSupported · Desktop & Mobile
Full support
Apple SafariSupported · macOS & iOS
Full support
Microsoft EdgeSupported · Chromium
Full support
Internet ExplorerNo native support · Use a polyfill
Polyfill
OperaSupported · Modern versions
Full support
Samsung InternetSupported · Android
Full support
Android WebViewSupported · Modern WebView
Full support
setAttributeNode()Baseline
Bottom line: Fully supported. Reach for it when cloning Attr nodes; otherwise setAttribute() is clearer.
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 (since July 2015). It is not Deprecated, Experimental, or Non-standard.
The replaced Attr node if one existed with the same name; otherwise null when nothing was replaced (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 belongs to at most 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).