Set the class attribute node of a <h1> element:
Before setting the attribute node:
After setting the attribute node:
More "Try it Yourself" examples below.
The setAttributeNode() method adds the specified attribute node to an element.
If the specified attribute already exists, this method replaces it.
The return value of this method is an Attr object. For more information, see The HTML DOM Attribute Object.
See also the setAttribute() method.
Tip: Use the removeAttributeNode() method to remove an attribute node from an element.
Method | |||||
---|---|---|---|---|---|
setAttributeNode() | Yes | Yes | Yes | Yes | Yes |
Parameter | Type | Description |
---|---|---|
attributenode | Attr object | Required. The attribute node you want to add |
Return Value: | An Attr object, representing the replaced attribute node, if any, otherwise null |
---|---|
DOM Version | Core Level 1 Element Object |
Set the href attribute node of a <a> element:
Before setting the attribute node:
After setting the attribute node:
HTML Tutorial: HTML Attributes
HTML DOM Reference: The HTML DOM Attribute Object
HTML DOM Reference: setAttribute() Method
HTML DOM Reference: document.createAttribute() Method
HTML DOM Reference: attribute.value Property
HTML DOM Reference: getAttributeNode() Method
HTML DOM Reference: removeAttributeNode() Method