XML DOM createElement()Methods


Document object

Definition and Usage

The createElement() method creates an element node.

This method returns an Element object.

Syntax

createElement(name)

Parameter Description
name A string that specifies the name for the element node.


Example

The following code snippet usesloadXMLDoc()put "books.xmlLoad into xmlDoc, and add an element node with a text node to each <book> element:

Example

xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.getElementsByTagName("book"); for (i=0;i<x.length;i++) { newel=xmlDoc.createElement("edition"); newtext=xmlDoc.createTextNode("first"); newel.appendChild(newtext); x[i].appendChild(newel); } //Output all title and edition y=xmlDoc.getElementsByTagName("title"); z=xmlDoc.getElementsByTagName("edition"); for (i=0;i<y.length;i++) { document.write(y[i].childNodes[0].nodeValue); document.write(" - Edition: "); document.write(z[i].childNodes[0].nodeValue); document.write("<br>"); }

Try it Yourself »

Document objectother extensions