Easy Tutorial
❮ Dom Met Node Hasattributes Dom Prop Document Doctype ❯

XML DOM createTextNode() Method


Definition and Usage

The createTextNode() method creates a text node.

This method returns a Text object.

Syntax

Parameter Description
text A string that specifies the text of the 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:

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 titles and editions
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>");
}

❮ Dom Met Node Hasattributes Dom Prop Document Doctype ❯