English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
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.
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‹/›
setAttributeNode() method is fully supported by all browsers:
Method | |||||
setAttributeNode() | Yes | Yes | Yes | Yes | Yes |
Parameter | Description |
---|---|
attr | The attribute node you want to add |
Return Value: | An Attr object representing the attribute node to be replaced (if any), otherwise null |
---|---|
DOM Version: | DOM Level1 |
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