XML DOM setAttributeNode()Methods


Element object

Definition and Usage

The setAttributeNode() method adds 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 attribute, the replaced attribute node is returned; otherwise, NULL is returned.

Syntax

elementNode.setAttributeNode(att_node)

Parameter Description
att_node Required. Specifies the attribute node to be set.


Example

The following code snippet usesloadXMLDoc()put "books.xmlLoad it into xmlDoc, and add the "edition" attribute to all <book> elements:

Example

xmlDoc=loadXMLDoc("books.xml"); newatt=xmlDoc.createAttribute("edition"); newatt.nodeValue="first"; x=xmlDoc.getElementsByTagName("title"); x[0].setAttributeNode(newatt); document.write("Edition: "); document.write(x[0].getAttribute("edition"));

Output:

Edition: first

Try it Yourself »

Element objectother extensions