Text.splitText() is an instance method that breaks one text node into two sibling Text nodes at a given offset. Learn the return value, how to insert an element between the halves, how normalize() joins them again, and common errors—with five examples and try-it labs.
01
Kind
Instance method
02
Returns
New Text node
03
Argument
offset index
04
Tree effect
Two sibling nodes
05
Opposite
normalize()
06
Status
Baseline widely
Fundamentals
Introduction
Sometimes you need a “cut” in the middle of a text node—so you can wrap part of the text, highlight a word, or insert a node between two halves. You cannot put an element inside a Text node. Instead, you split the text, then insert between the two resulting siblings.
JavaScript
const text = new Text("foobar");
const second = text.splitText(3);
console.log(text.data); // "foo"
console.log(second.data); // "bar"
💡
Beginner tip
MDN: separated text nodes can be concatenated again with Node.normalize(). Think of splitText and normalize as opposite operations.
Concept
Understanding the Method
MDN: splitText() breaks the Text node into two nodes at the specified offset, keeping both nodes in the tree as siblings. After the split:
Original node — keeps content up to the offset.
New node — holds the remaining text; this is the return value.
Parent — if present, the new node is inserted as the next sibling.
Offset === length — the new node has empty data.
Foundation
📝 Syntax
JavaScript
const after = textNode.splitText(offset);
Parameters
Name
Type
Description
offset
Number
Index immediately before which to break the text node (0-based, like string indices for BMP characters).
Return value
The newly created Text node that contains the text after the offset.
Exceptions
IndexSizeError — offset is negative or greater than the number of 16-bit units in the node’s text.
NoModificationAllowedError — the node is read-only.
Compare
🔄 splitText() vs normalize()
splitText(offset)
normalize()
Effect
One Text → two siblings
Adjacent Text siblings → one
Returns
New Text (after offset)
undefined
Called on
A Text node
Usually a parent / ancestor Node
Typical use
Cut so you can insert between
Tidy the tree after edits
After a split, wholeText on either sibling still reads the full adjacent run until you insert a non-Text node between them.
Text.splitText() is Baseline Widely available across modern browsers (MDN: since July 2015). Logos use the shared browser-image-sprite.png sprite from this project.
✓ Baseline · Widely available
Text.splitText()
Split a text node at an offset into two sibling Text nodes — great for mid-text inserts.
UniversalWidely available
Google ChromeFull support · Desktop & Mobile
Full support
Mozilla FirefoxFull support · Desktop & Mobile
Full support
Apple SafariFull support · macOS & iOS
Full support
Microsoft EdgeFull support · Chromium
Full support
OperaFull support · Modern versions
Full support
Internet ExplorerSupported in modern IE versions
Supported
splitText()Excellent
Bottom line: Use splitText(offset) to cut a Text node, insertBetween with insertBefore(returnedNode), and normalize() to merge adjacent text again.
Wrap Up
Conclusion
text.splitText(offset) cuts a text node into two siblings and returns the second half—perfect for inserting elements mid-string. Pair it with insertBefore, and use normalize() when you want one text node again.
Keep the returned node as your insertBefore reference
Check 0 <= offset <= text.length before splitting
Call normalize() when you no longer need the cut
Use wholeText to verify adjacent runs after a split
Prefer DOM methods over innerHTML for mid-text wraps
❌ Don’t
Pass a negative offset or one past length
Expect to nest an element inside a Text node without splitting
Forget the new node is already in the tree when a parent exists
Confuse splitText with String.prototype.split
Leave many empty text siblings if you can normalize instead
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about splitText()
Cut a Text node; insert between the halves.
5
Core concepts
✂️01
Instance
method on Text
Kind
📄02
Returns Text
after the cut
Result
📌03
Next sibling
when parented
DOM
⚖️04
Opposite
normalize()
Pair
🎯05
Baseline
widely available
Status
❓ Frequently Asked Questions
It breaks the Text node into two sibling Text nodes at the given offset. The original keeps the text before the offset; the method returns a new Text node with the rest. If the original had a parent, the new node is inserted as the next sibling.
No. MDN marks Text.splitText() as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard.
The index immediately before which to break. splitText(3) on "foobar" leaves "foo" in the original node and returns a node with "bar".
IndexSizeError if offset is negative or greater than the node’s length (16-bit units). NoModificationAllowedError if the node is read-only.
Call parent.normalize() (or normalize on an ancestor). MDN describes Node.normalize() as the opposite: it merges adjacent text nodes again.
The original keeps all characters; the returned Text node has empty data (""), and is still inserted as the next sibling when a parent exists.
Did you know?
String.prototype.split returns an array of strings and does not change the DOM. Text.splitText mutates the live document tree. Same English word, completely different APIs.