Skip to content
Related Articles

Related Articles

HTML DOM splitText() method
  • Last Updated : 03 Jul, 2020

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
My Personal Notes arrow_drop_up
Recommended Articles
Page :