JavaScript Text splitText() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Method

Definition

Text.splitText(offset) breaks one Text node into two siblings at the given index (MDN). The original keeps the left half; the method returns the new right-half node.

Pass the required arguments shown below. Check return values and browser support before you rely on the result.

Syntax

Call text.splitText. Start with this line.

JavaScript
splitText(offset)
CallWhat it does
splitText(offset)Primary signature.
text.splitText(...)Call with the arguments your case needs.

Built into the browser. Call splitText(...) on a Text node — no import required.

Library declarations
JavaScript
splitText(offset)

Parameters

ParameterDescription
offsetRequired. Index immediately before the break (UTF-16 units)

Return Value

Return ValueDescription
*New Text with the right-hand characters

JavaScript Examples

Example 1 — Basic Split

Split "foobar" at offset 3.

JavaScript
const text = new Text("foobar");
const bar = text.splitText(3);

console.log(text.data); // foo
console.log(bar.data);  // bar
Try It Yourself

How It Works

  1. Create a Text node. Content is "foobar".
  2. Split at 3. Index 3 is right before "b".
  3. Left stays. text.data becomes "foo".
  4. Right returns. bar.data is "bar".

Example 2 — Next Sibling

With a parent, the returned node is inserted as the next sibling.

JavaScript
const p = document.createElement("p");
const foobar = new Text("foobar");
p.appendChild(foobar);

const bar = foobar.splitText(3);

console.log(p.childNodes.length);     // 2
console.log(foobar.nextSibling === bar); // true
console.log(p.textContent);           // foobar
Try It Yourself

How It Works

  1. Append first. One text child is attached to <p>.
  2. Split. The tree gains a second text child.
  3. Check siblings. nextSibling points at the returned node.
  4. Visible text. Combined textContent is still "foobar".

Example 3 — Insert Between Halves

MDN pattern: place an element between the split parts.

JavaScript
const p = document.createElement("p");
p.appendChild(new Text("foobar"));

const bar = p.firstChild.splitText(3);
const u = document.createElement("u");
u.appendChild(new Text(" new content "));
p.insertBefore(u, bar);

console.log(p.innerHTML);
// foo<u> new content </u>bar
Try It Yourself

How It Works

  1. Split. Get a handle to the right-hand text node.
  2. Build markup. Create <u> with its own text child.
  3. Insert before the right half. That slots the element in the middle.
  4. Result. Left text, element, then right text.

Example 4 — Offset Equals Length

Splitting at the end returns an empty Text node.

JavaScript
const text = new Text("hi");
const empty = text.splitText(text.length);

console.log(text.data);   // hi
console.log(empty.data);  // ""
console.log(empty.length); // 0
Try It Yourself

How It Works

  1. Use length as offset. That is allowed (unlike length + 1).
  2. Left unchanged. All characters stay in the original node.
  3. Right is empty. Returned node has data === "".
  4. Still a Text. Useful as an insert anchor at the end.

Example 5 — IndexSizeError

Offsets outside 0…length throw.

JavaScript
const text = new Text("abc");

try {
  text.splitText(10);
} catch (e) {
  console.log(e.name); // IndexSizeError
}

try {
  text.splitText(-1);
} catch (e) {
  console.log(e.name); // IndexSizeError
}
Try It Yourself

How It Works

  1. Valid range. Offset must be from 0 through length inclusive.
  2. Too large. Greater than length → IndexSizeError.
  3. Negative. Also throws IndexSizeError.
  4. Guard with try/catch. Or clamp the offset before calling.

Browser Support

Text.splitText() is marked Baseline Widely available on MDN (since July 2015). DOM text API — not a Node.js built-in.

✓ Baseline · Widely available

splitText()

Split Text nodes across modern browsers. Match offset carefully; merge again with normalize().

98% Core tag support
Chrome 1+
Full support
Edge 12+
Full support
Firefox 1+
Full support
Safari 1+
Full support
Opera ✓
Full support
HTML element 100% supported

Bottom line: Safe for production. Use splitText(offset) to break a Text node; call parent.normalize() to merge siblings again.

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 (counted in UTF-16 code units). NoModificationAllowedError if the node is read-only.
Call parent.normalize() (or normalize on an ancestor). MDN describes Node.normalize() as the way to merge 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.

Next: assignedSlot

See which slot a Text node is assigned to.

assignedSlot →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful