A Text node holds character data in the DOM—the letters between tags. It is a Node with nodeType 3, not an Element.
Create one with document.createTextNode("Hello") or new Text("Hello"). splitText(offset) splits a node in two. wholeText joins adjacent text nodes’ contents for reading.
This page teaches what Text is. Constructor, methods, and properties live under this section.
textContent vs Text nodes.
Setting element.textContent is usually easier. Use Text nodes when you need split/merge control or precise tree edits.
Syntax
JavaScript Syntax
Creating and reading a Text node:
JavaScript
const text = document.createTextNode("Hello");
console.log(text.nodeType); // 3
console.log(text.data);
p.appendChild(text);
Reference
Core Ideas
Ideas every Text tutorial builds on:
Idea
Example
Remember
nodeType 3
text.nodeType
Identifies text nodes
Create
document.createTextNode("Hi")
Or new Text("Hi")
data
text.data
The character string
splitText
text.splitText(2)
Breaks into two nodes
wholeText
text.wholeText
Adjacent text combined
Not Element
no tagName
No attributes or classList
Code
JavaScript Examples
Three short programs that show how Text works.
Example 1 — Create a text node
createTextNode returns a Text instance.
JavaScript
const text = document.createTextNode("Hello");
console.log(text.data);
console.log(text.nodeType);
Output
Hello
3
How It Works
createTextNode("Hello") builds the node.
data holds the characters.
nodeType is 3.
Example 2 — Split at an offset
splitText divides one Text into two siblings.
JavaScript
const text = document.createTextNode("Hello");
const second = text.splitText(2);
console.log(text.data);
console.log(second.data);