English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

JS DOM Reference Manual

HTML DOM Element Object

setAttributeNode()HTML DOM setAttributeNode() method

method adds a new Attr node to the specified element.

If the element already has a named attribute, replace it with the new attribute and return the replaced attribute.removeAttributeNode()method removes an attribute node from an element.

You can find methods to remove attribute nodes from elements in ourIn the HTML DOM attribute objectLearn more about the Attr object.

Syntax:

element.setAttributeNode(attr);
var elem = document.getElementsByTagName("H");1")[0];
var attr = document.createAttribute("class"); // Create a "class" attribute
attr.value = "demo"; // Set the value of the class attribute
elem.setAttributeNode(attr); // Add the class attribute to <h1>
Test to see‹/›

Browser Compatibility

setAttributeNode() method is fully supported by all browsers:

Method
setAttributeNode()YesYesYesYesYes

Parameter Value

ParameterDescription
attrThe attribute node you want to add

Technical Details

Return Value:An Attr object representing the attribute node to be replaced (if any), otherwise null
DOM Version:DOM Level1

Related References

HTML Tutorial:HTML Attributes

HTML DOM Reference:HTML DOM Attribute Object

HTML DOM Reference:element .getAttribute() method

HTML DOM Reference:element .setAttribute() method

HTML DOM Reference:element .removeAttributeNode() method

HTML DOM Element Object