HTML DOM Attributes setNamedItem()
Examples
Set a H1's class attribute:
const nodeMap = document.getElementsByTagName("H1")[0].attributes;
const node = document.createAttribute("class");
node.value = "democlass";
nodeMap.setNamedItem(node);
Try it Yourself »
It is easier to use element.setAttribute():
const element = document.getElementsByTagName("H1")[0];
element.setAttribute("class", "democlass");
Try it Yourself »
Definition and Usage
The setNamedItem()
method adds an attribute node to a NamedNodeMap.
If the attribute node already exists, it will be replaced, and the replaced attribute node is returned,
otherwise the return value is null
.
Alternative:
It is easier to use the element.setAttribute() method.
See Also:
The attributes.getNamedItem() Method
Syntax
namednodemap.setNamedItem(node)
Parameters
Parameter | Description |
node | Required. The node to add or replace in the NamedNodeMap. |
Return Value
Type | Description |
A node | The replaced node (if any). Otherwise null . |
Browser Support
attributes.setNamedItem
is a DOM Level 1 (1998) feature.
It is fully supported in all browsers:
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | 9-11 | Yes | Yes | Yes | Yes |