Easy Tutorial
❮ Dom Prop Element Tagname Prop Attr Localname ❯

XML DOM createElement() Method



Definition and Usage

The createElement() method creates an element node.

This method returns an Element object.

Syntax

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

Example

The following code snippet uses loadXMLDoc() to load "books.xml" into xmlDoc and adds an element node with a text node to each <book> element:

Example

xmlDoc=loadXMLDoc("books.xml");

x=xmlDoc.getElementsByTagName("book");

for (i=0;i&lt;x.length;i++)
{
  newel=xmlDoc.createElement("edition");
  newtext=xmlDoc.createTextNode("first");
  newel.appendChild(newtext);
  x[i].appendChild(newel);
}

// Output all titles and editions
y=xmlDoc.getElementsByTagName("title");
z=xmlDoc.getElementsByTagName("edition");
for (i=0;i&lt;y.length;i++)
{
  document.write(y[i].childNodes[0].nodeValue);
  document.write(" - Edition: ");
  document.write(z[i].childNodes[0].nodeValue);
  document.write("<br>");
}

❮ Dom Prop Element Tagname Prop Attr Localname ❯