The splitText() method breaks the Text node into two nodes at the specified offset index, keeping both nodes in the tree as siblings.
After Splitting the text, the main node contains all the content up to the specified offset index point, and a newly created node of the same type contains the remaining text.
Syntax:
newTextNode=textNode.splitText(offsetIndex)
Parameters:
- offset index: The index immediately before which to break the text node.
Return Value:
- Returns the new created Text node that contains the text after the offset index.
Example:
<html> <head> <title>HTML | DOM textSplit() method</title> <p id="h">GeeksforGeeks</p> <script> // get the p element const a = document.getElementById('h'); // get the textcontent in textnode const firstNode = a.firstChild; // splitting the firstnode at 5th offset index const newNode = firstNode.splitText(5); console.log(firstNode); console.log(newNode); </script> </head> </html> |
Output:
In console, the two splitted text Nodes can be seen.
Supported Browsers:
- Google Chrome
- Edge
- Firefox
- Opera
Recommended Posts:
- PHP | DOMText splitText() Function
- HTML | DOM HTML Object
- HTML | DOM isEqualNode() Method
- HTML | DOM History go() Method
- HTML | DOM Input Date stepUp() Method
- HTML | DOM console.warn() Method
- HTML | DOM console.clear() Method
- HTML | DOM blur() Method
- HTML | DOM createElement() Method
- HTML DOM queueMicrotask() Method
- HTML | DOM Location replace() Method
- HTML | DOM close() Method
- HTML | DOM createAttribute() Method
- HTML | DOM writeln() Method
- HTML | DOM console.trace() Method
- HTML | DOM createComment() Method
- HTML | DOM console.table() Method
- HTML | DOM console.time() Method
- HTML | DOM createTextNode() Method
- HTML | DOM console.error() Method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


