Text
Baseline
Widely available
*
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
* Some parts of this feature may have varying levels of support.
The Text interface represents a text node in a DOM tree.
To understand what a text node is, consider the following document:
<html lang="en" class="e">
<head>
<title>Aliens?</title>
</head>
<body>
Why yes.
</body>
</html>
In that document, there are five text nodes, with the following contents (all spaces are shown as ◦ and all newlines as ⏎):
"⏎◦◦◦◦"(after the<head>start tag, a newline followed by four spaces)"Aliens?"(the contents of the<title>element)"⏎◦◦"(after the</title>end tag, a newline followed by two spaces)"⏎◦◦"(after the</head>end tag, a newline followed by two spaces)"⏎◦◦◦◦Why◦yes.⏎◦◦⏎"(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
Textnode with the parameter as its textual content.
Instance properties
Inherits properties from its parents, CharacterData, Node, and EventTarget.
Text.assignedSlotRead only-
Returns a
HTMLSlotElementrepresenting the<slot>the node is inserted in. Text.wholeTextRead only-
Returns a string containing the text of all
Textnodes logically adjacent to thisNode, concatenated in document order.
Instance methods
Inherits methods from its parent, CharacterData, Node, and EventTarget.
Text.splitText-
Breaks the node into two nodes at a specified offset.
Specifications
| Specification |
|---|
| DOM> # interface-text> |