HTML DOM setNamedItem()Method
Example
Set the class attribute of H1:
var btn=document.getElementsByTagName("h1")[0];
var typ=document.createAttribute("class");
typ.nodeValue="democlass";
btn.attributes.setNamedItem(typ);
var typ=document.createAttribute("class");
typ.nodeValue="democlass";
btn.attributes.setNamedItem(typ);
Before setting attribute:
Hello world
After setting attribute:
Hello world
Try it yourself »
Definition and Usage
The setNamedItem() method is used to add a specified node.
If the node already exists, it will be replaced, and the value of the replaced node is returned; otherwise, null is returned.
Browser Support
![]()
![]()
![]()
![]()
![]()
All major browsers support the setNamedItem() method
Syntax
namednodemap.setNamedItem(node)
Parameters
| Parameter | Type | Description |
|---|---|---|
| node | Node object | Required. The node in the node list that you want to replace. |
Return Value
| Type | Description |
|---|---|
| Node object | Returns the replaced node, or null if no replacement was made |
Technical Details
| DOM Version | Core Level 1 |
|---|
Other Extensions
Attribute Object