HTML DOM splitText() method

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<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>

chevron_right


Output:

In console, the two splitted text Nodes can be seen.

Supported Browsers:

  • Google Chrome
  • Edge
  • Firefox
  • Opera

full-stack-img

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.