Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

HTML DOM setAttributeNode() method

HTML DOM setAttributeNode() method

Element Object Reference Element object

Example

var atr=document.createAttribute("class");

atr.nodeValue="democlass";

document.getElementsByTagName("H1")[0].setAttributeNode(atr);

Before setting the attribute node:

Hello World

After setting the attribute node:

Hello World

Try it »

Definition and Usage

The setAttributeNode() method is used to add a new attribute node.

If an attribute with the specified name already exists in the element, that attribute will be replaced by the new attribute. If the new attribute replaces an existing one, the replaced attribute is returned, otherwise NULL is returned.


Browser support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

All major browsers support the setAttributeNode() method


Syntax

element

.setAttributeNode(

attributenode

)

Parameters

Parameters Type Description
attributenode Attr object Required. The attribute node you want to add.

Return Value

Type Description
Attr object The replaced attribute node, or NULL if none null

Technical Details

DOM version Core Level 1 Element Object

Element Object Reference Element object