node.textContent is the plain text of a node and its descendants (MDN). You can read it or assign a new string.
Status: Baseline Widely available (since July 2015) — not Deprecated, Experimental, or Non-standard.
Warning: Setting textContent removes all child nodes and replaces them with one text node (MDN).
Remember
el.textContent → "plain text from subtree"
el.textContent = "Hi" → children wiped; one text node left
Prefer for plain text (safer than innerHTML)
vs innerText: no layout / hidden text still included
Document / DocumentType → null
Read the string, or assign to replace the node’s children with text.
Select an element (or other node).
Read el.textContent for concatenated descendant text.
Assign a string to set plain text (children are replaced).
Use document.documentElement.textContent for whole-page text — not document.textContent.
Piece
Type
Role
node
Node
Node to read or update
Get
string or null
Subtree text (or null for Document / doctype)
Set
string
Replace all children with one text node
Pseudocode
Pseudocode
// get
if node is Document or DocumentType:
return null
if node is Text / Comment / CDATA / PI:
return nodeValue
else:
return join(textContent of children)
(skip comments & processing instructions)
// set
remove all children
append one Text node with the string
Reference
Cheat Sheet
Keep these text facts handy.
Topic
Detail
Syntax
node.textContent / node.textContent = s
Get returns
string or null
Set effect
Wipes children → one text node (MDN)
vs innerText
Ignores CSS; includes hidden; no reflow
vs innerHTML
Plain text; safer for untrusted strings
Document
null — use documentElement
Standard
Baseline Widely available (since July 2015)
Try it
Live Preview
Read nested text, then assign a new string and watch children get replaced.
This is some text!
Browser demo of Node.textContent — set replaces all children.
Result
Click Read or Set…
Code
JavaScript Examples
Short, copy-ready snippets. Open DevTools Console, or use Try It Yourself for a live editor.
Example 1 — Read Nested Text
Tags disappear; descendant text is concatenated (MDN).
JavaScript
const el = document.getElementById("divA");
// <div id="divA">This is <span>some</span> text!</div>
console.log(el.textContent);
// "This is some text!"
Tags are not included. Only the text nodes matter.
Comments skipped. Processing instructions are skipped too (MDN).
Example 2 — Set Replaces Children
Assignment wipes nested elements and leaves one text node.
JavaScript
const el = document.getElementById("divA");
el.textContent = "This text is different!";
console.log(el.childNodes.length); // 1
console.log(el.firstChild.nodeName); // "#text"
console.log(el.innerHTML); // This text is different!
Assign a string. Old children are removed (MDN warning).
One text node remains. That is the new content.
Nested spans are gone. Do not set if you need to keep markup.
Use for labels / messages. Ideal when you only need plain text.
Example 3 — Safer Than innerHTML for Text
Untrusted strings stay text — they are not parsed as HTML (MDN).
JavaScript
const el = document.getElementById("msg");
const user = "<img src=x onerror=alert(1)>";
el.textContent = user; // shows the characters as text
// el.innerHTML = user; // would parse HTML — avoid for untrusted data
console.log(el.textContent);
console.log(el.querySelector("img")); // null
Assign untrusted input. Treated as characters, not markup.
No HTML parser. Angle brackets stay visible as text.
No injected elements.querySelector("img") finds nothing.
Prefer for plain text UI. Use innerHTML only when you truly need HTML.
Edge Cases & Pitfalls
Check these before swapping textContent, innerText, and innerHTML.
set
Setting wipes children
MDN warning: assignment removes every child and inserts one text node. Nested buttons, spans, and listeners on those nodes are gone.
document
document.textContent is null
Use document.documentElement.textContent for the whole document’s text (MDN).
vs
Not the same as innerText
textContent includes hidden text and skips layout. innerText is CSS-aware and can trigger reflow.
xss
Prefer over innerHTML for text
MDN: innerHTML parses markup and can be XSS-prone. Use textContent for plain strings.
Compatibility
Browser Support
Node.textContent is marked Baseline Widely available on MDN (since July 2015).
✓ Baseline · Widely available
Node.textContent
Get or set the text content of a node and its descendants. Setting replaces all children with one text node. Prefer over innerHTML for plain text. Not Deprecated, Experimental, or Non-standard.
Prefer textContent when you do not need CSS-aware text or HTML parsing — it avoids reflow and the HTML parser.
Remember
Key Takeaways
Plain text: get concatenated descendant text; set replaces children.
Safer for strings: prefer over innerHTML for untrusted text.
vs innerText: no layout; includes hidden content.
Document → null: use documentElement.textContent.
One line:textContent is the plain-text get/set for a node — assignment replaces all children.
Frequently Asked Questions
For most elements, a string that concatenates the text of all descendant text nodes (excluding comments). For text/comment nodes it is the node’s own text. For Document or DocumentType it returns null.
No. MDN marks Node.textContent as Baseline Widely available (since July 2015). It is a standard DOM property — not Deprecated, Experimental, or Non-standard.
All child nodes are removed and replaced with a single text node containing the string you assign. Nested elements inside are gone.
textContent returns all text in the subtree, including hidden content, and does not require layout. innerText respects CSS visibility/styling and can trigger reflow.
textContent deals with plain text. innerHTML parses HTML and can introduce XSS risk if you insert untrusted strings. Prefer textContent for user-facing plain text updates.
MDN: for a Document or DocumentType, textContent returns null. Use document.documentElement.textContent for the whole page’s text.