X
HOME HTML5 CSS3 JAVASCRIPT SQL PHP5 JQUERY ANGULAR BOOTSTRAP3 XML MORE...
REFERENCES | EXAMPLES | FORUM | ABOUT

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


Your suggestion:

Close [X]

Thank You For Helping Us!

Your message has been sent to W3Schools.

Close [X]
Search w3schools.com:

WEB HOSTING

UK Reseller Hosting

WEB BUILDING

Download XML Editor FREE Website BUILDER Free HTML5 Templates

SHARE THIS PAGE

facebook