XML DOM createTextNode() Method

Document object reference manual

Definition and Usage

createTextNode() can create a text node.

This method can return Text object.

Syntax:

createTextNode(Data)
Parameter Description
Data String value, which can specify the text of this node.

Return value

Returns a newly created Text node representing the specified Data String.

Example

In all examples, we will use the XML file books.xml, as well as the JavaScript function loadXMLDoc().

The following code snippet can add an element node with a text node to each <book> element:

xmlDoc=loadXMLDoc("/example/xdom/books.xml");
var x=xmlDoc.getElementsByTagName('book');
var newel,newtext
for (i=0;i<x.length;i++)
  {
  newel=xmlDoc.createElement('edition');
  newtext=xmlDoc.createTextNode('First');
  newel.appendChild(newtext);
  x[i].appendChild(newel);
  }

Document object reference manual