Text.splitText(offset)
The splitText method splits a Text node into two at the specified offset. It
places all the characters from the offset to the end of the string into a new Text
node that immediately follows this one. If the offset is 0, the entire contents of this
string are copied into the new node leaving the original one empty. If the offset is beyond
the last character of this string, then a new, empty Text node is
created. This method returns the new Text node.
The code in the following example first loads the 'currencies.xml' file. Then it splits
the text of the second 'currency' element into two at offset 4 (the fifth character in the
string, which will become the first character of the new Text
node). Finally the code iterates through the child nodes of the 'currency' element (now
numbering 2) and displays the data property of each.
XML:
<currencies>
<currency>CHF Swiss Francs</currency>
<currency>DEM German Deutsche Marks</currency>
<currency>GBP United Kingdom Pounds</currency>
<currency>JPY Japanese Yen</currency>
<currency>USD United States Dollars</currency>
</currencies>
Code (JavaScript):
xml_doc = new ActiveXObject("Microsoft.XMLDOM");
xml_doc.async = false;
xml_doc.load("currencies.xml");
elem = xml_doc.documentElement.childNodes.item(1);
text = elem.firstChild
text.splitText(4);
len = elem.childNodes.length;
for (i = 0; i < len; i++)
document.write(elem.childNodes.item(i).data + "<br>");
Output:
DEM
German Deutsche Marks
|