XML DOM splitText() Method
Definition and Usage
The splitText() method splits the text node into two nodes according to the specified offset.
Syntax:
replaceData(offset)
Parameter | Description |
---|---|
offset | Required. Specifies where to split the text node. The starting value is 0. |
Return value
The Text node split from the current node.
Description
This method will split the Text node at the specified offset into two nodes. The original Text node will be modified to contain the text content before the offset position specified (but not including the text content). A new Text node will be created to store all characters from the offset position (including the character at that position) to the end of the original character. The new Text node is the return value of this method. In addition, if the original Text node has a parentNode, the new Text node will be inserted into this parent node immediately after the original node.
CDATASection InterfaceCDATASection interface inherits the Text interface, and CDATASection nodes can also use this method, but the newly created node is a CDATASection node, not a Text node.
Example
In all examples, we will use the XML file books.xml, as well as JavaScript functions loadXMLDoc().
The following code snippet will split the Text node after the first word:
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("title")[0].childNodes[0];
y=x.splitText(9);
document.write(x.nodeValue);
document.write("<br />");
document.write(y.nodeValue);
Output:
Everyday Italian