dom / latest / text.html /

Text

The Text interface represents a text node in a DOM tree.

EventTarget Node CharacterData Text

To understand what a text node is, consider the following document:

<html class="e"><head><title>Aliens?</title></head>
 <body>Why yes.
</body></html>

In that document, there are three text nodes, with the following contents:

  • "Aliens?" (the contents of the title element)
  • "\n" (after the </head> end tag, a newline followed by a space)
  • "Why yes.\n" (the contents of the body element)

Each of those text nodes is an object that has the properties and methods documented in this article.

Constructor

Text()

Returns a new Text node with the parameter as its textual content.

Properties

Inherits properties from its parents, CharacterData, Node, and EventTarget.

Text.assignedSlot Read only

Returns a HTMLSlotElement representing the <slot> the node is inserted in.

Text.wholeText Read only

Returns a DOMString containing the text of all Text nodes logically adjacent to this Node, concatenated in document order.

Methods

Inherits methods from its parent, CharacterData, Node, and EventTarget.

Text.replaceWholeText Deprecated

Replaces the text of the current node and all logically adjacent nodes with the specified text.

Text.splitText

Breaks the node into two nodes at a specified offset.

Specifications

Browser compatibility

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari WebView Android Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet
Text
1
12
1
5
≤12.1
1
1
18
4
≤12.1
1
1.0
Text
28
16
24
No
15
8
4.4
28
24
14
8
2.0
assignedSlot
53
79
63
No
40
10.1
53
53
63
41
10.3
6.0
replaceWholeText
1-45
12-79
1-10
9
15-32
4-10.1
≤37-45
18-45
4-10
14-32
3-10.3
1.0-5.0
splitText
1
Before Chrome 30, the offset parameter was optional.
12
1
5
≤12.1
Before Opera 17, the offset parameter was optional.
1
The offset parameter is optional.
1
Before version 4.4, the offset parameter was optional.
18
Before Chrome 30, the offset parameter was optional.
4
≤12.1
Before Opera 17, the offset parameter was optional.
1
The offset parameter is optional.
1.0
Before Samsung Internet 2.0, the offset parameter was optional.
wholeText
1
12
3.5
9
≤12.1
4
1
18
4
≤12.1
3.2
1.0
getBoxQuads
No
No
31
No
No
No
No
No
No
No
No
No

See also

© 2005–2021 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/Text