splitText (W3C DOM Core method)

Spec
Version Depr. Static
DOM1 No No
Browser support (more…)
IE5.5+ FF1.5+ SA1.3+ OP9+
Full Full Full Full
Returns
Text
Throws
INDEX_SIZE_ERR, NO_MODIFICATION_ALLOWED_ERR

Example

var text = document.getElementById('lyric').firstChild;

text.splitText(28);

In the example above we get a reference to the firstChild Text node of an element, then we split that node at character offset 28.

So if the original element was a quotation with a single child Text node, like this:

<q>One thing I can tell you is you've got to be free</q>

Then the end result of the operation above would be a quotation with two sibling Text nodes, the first of which has the value One thing I can tell you is , and the second has the value you've got to be free.

Arguments

offset (unsigned long) required

The character offset at which to split this node.

Description

Break a Text node into two nodes, at the specified character offset.

A new node will be created as the nextSibling of the first, containing everything from the offset point up to the end of the string; the original node will now contain just the text before the offset.

Tip: This method is usually redundant in JavaScript

I've never used this method — JavaScript's built-in string manipulation methods are far more flexible and powerful - unless you specifically want to create multiple sibling Text nodes.

Return value

The newly created text node

Compatibility

Internet Explorer Firefox Safari Opera
5.5 6.0 7.0 1.5 2.0 3.0 1.3 2.0 3.0 9.0 9.5
Full Full Full Full Full Full Full Full Full Full Full

No known issues.

User-contributed notes

There are no comments yet.

Related Products