splitText method
Divides a text node at the specified index.
![]() |
Syntax
var retval = TextNode.splitText(offset);Parameters
- offset [in, optional]
-
Type: Integer
A Integer that specifies the index of the string that indicates where the separation occurs. If a value is not provided, a new text node with no value is created.
Return value
Type: IHTMLDOMNode
Returns a text node object.
Standards information
Remarks
The text node that invokes the splitText method has a nodeValue equal to the substring of the value, from zero to offset. The new text node has a nodeValue of the substring of the original value, from the specified index to the value length. Text node integrity is not preserved when the document is saved or persisted.
Examples
This example demonstrates the splitText method.
<!DOCTYPE html> <html> <head> <title>splitText</title> </head> <body> <div id="divContainer">ABCDEFGH</div> <p> <button>Click to set the last two non-bold characters to bold.</button> </p> <script> document.getElementsByTagName('button')[0].addEventListener('click', setBold, false); function setBold(evt) { var divContainer = document.getElementById('divContainer'); var textNode = divContainer.firstChild; if (textNode.data.length == 0) { return; } var textNodeRight = textNode.splitText(textNode.data.length - 2); var strongElement = document.createElement('strong'); divContainer.insertBefore(strongElement, textNodeRight); strongElement.appendChild(textNodeRight); } </script> </body> </html>
See also
Show:
