HTML DOM setNamedItem() method
Example
Set the class attribute of the H1:
var btn=document.getElementsByTagName("h1")[0];
var typ=document.createAttribute("class");
typ.nodeValue="democlass";
btn.attributes.setNamedItem(typ);
Before setting the attribute:
Hello world
After setting the attribute:
Hello world
Try it »
Definition and Usage
The setNamedItem() method is used to add the specified node.
If the node already exists, it will be replaced, and the replaced node's value is returned; otherwise null is returned.
Browser support





All major browsers support the setNamedItem() method
Syntax
namednodemap
.setNamedItem(
node
)
Parameters
| Parameters | Type | Description |
|---|---|---|
| node | Node object | Required. The node you want to replace in the node list. |
Return Value
| Type | Description |
|---|---|
| Node object | Returns the replaced node, or null if nothing was replaced |
Technical Details
| DOM version | Core Level 1 |
|---|
Attribute object