THE WORLD'S LARGEST WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

HTML DOM setNamedItem() Method

Attribute Object Reference Attribute Object

Example

Set a H1's class attribute:

var btn = document.getElementsByTagName("H1")[0];
var typ = document.createAttribute("class");
typ.value = "democlass";
btn.attributes.setNamedItem(typ);

Before setting the attribute:

Hello world

After setting the attribute:

Hello world

Try it yourself »

Definition and Usage

The setNamedItem() method adds the specified node to the NamedNodeMap.

If the node already exists, it will be replaced, and the replaced node will be the return value, otherwise the return value will be null.

Tip: Instead of working with attribute nodes, you could use the element.setAttribute() method to add an attribute with a value to an element.


Browser Support

Method
setNamedItem() Yes Yes Yes Yes Yes

Syntax

namednodemap.setNamedItem(node)

Parameter Values

Parameter Type Description
node Node object Required. The node you want to add/replace in the NamedNodeMap collection

Technical Details

Return Value: A Node object, representing the replaced node (if any), otherwise null
DOM Version Core Level 1

Attribute Object Reference Attribute Object