JavaScript textNode.splitText() Method

You are Here:

JavaScript textNode.splitText() Method

The textNode.splitText() method breaks the Text node into two nodes at the specified offset, keeping both nodes in the tree as siblings.

After the split, the current node contains all the content up to the specified offset point, and a newly created node of the same type contains the remaining text.

Example

HTML Online Editor
<!DOCTYPE html> <html> <body> <p>Hard fail</p> <p>Click on the button to split text and insert new text in it.</p> <button onclick="myFunction()">Click Me</button> <p id="point"></p> <script> var p = document.getElementsByTagName("p")[0]; var textnode = p.firstChild; function myFunction(){ var replacementNode = textnode.splitText(4); var span = document.createElement("span"); span.appendChild(document.createTextNode(" work never")); p.insertBefore(span, replacementNode); } </script> </body> </html>

Syntax

TextNode.splitText(offset)

Parameter Values

ValueTypeExplanation
offsetRequiredA number specifies the position to split the text into two nodes.

Reminder

Hi Developers, we almost covered 97% of JavaScript Tutorials with examples for quick and easy learning.

We are working to cover every Single Concept in JavaScript.

Please do google search for:

Join Our Channel

Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.

This channel is primarily useful for Full Stack Web Developer.

Share this Page

Meet the Author