-
Notifications
You must be signed in to change notification settings - Fork 22.4k
/
index.md
92 lines (64 loc) · 2.29 KB
/
index.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
---
title: "Text: splitText() method"
short-title: splitText()
slug: Web/API/Text/splitText
page-type: web-api-instance-method
browser-compat: api.Text.splitText
---
{{APIRef("DOM")}}
The **`splitText()`** method of the {{domxref("Text")}} interface
breaks the {{domxref("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.
The newly created node is returned to the caller.
If the original node had a parent, the new node is inserted as the next sibling of the original node.
If the offset is equal to the length of the original node,
the newly created node has no data.
Separated text nodes can be concatenated using the {{domxref("Node.normalize()")}}
method.
## Syntax
```js-nolint
newNode = textNode.splitText(offset)
```
### Parameters
- `offset`
- : The index immediately before which to break the text node.
### Return value
Returns the newly created {{domxref("Text")}} node that contains the text after the
specified offset point.
### Exceptions
- `IndexSizeError` {{domxref("DOMException")}}
- : Thrown if the specified offset is negative or is greater
than the number of 16-bit units in the node's text.
- `NoModificationAllowedError` {{domxref("DOMException")}}
- : Thrown if the node is read-only.
## Example
In this example, the text of a {{HTMLElement("p")}} is split into two text nodes, and a
{{HTMLElement("u")}} is inserted between them.
```html
<p>foobar</p>
```
```js
const p = document.querySelector("p");
// Get contents of <p> as a text node
const foobar = p.firstChild;
// Split 'foobar' into two text nodes, 'foo' and 'bar',
// and save 'bar' as a const
const bar = foobar.splitText(3);
// Create a <u> element containing ' new content '
const u = document.createElement("u");
u.appendChild(document.createTextNode(" new content "));
// Add <u> before 'bar'
p.insertBefore(u, bar);
// The result is: <p>foo<u> new content </u>bar</p>
```
{{EmbedLiveSample("Example", 700, 70)}}
## Specifications
{{Specifications}}
## Browser compatibility
{{Compat}}
## See also
- The {{domxref("Text")}} interface it belongs to.
- The opposite method: {{domxref("Node.normalize")}}.