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 »

Description

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.


Syntax

namednodemap.setNamedItem(node)

Parameters

Parameter Description
node Required.
The node to add or replace in the NamedNodeMap.

Return Value

Type Description
A nodeThe 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 Edge Firefox Safari Opera IE
Yes Yes Yes Yes Yes 9-11


Copyright 1999-2023 by Refsnes Data. All Rights Reserved.