The Node.textContent property gets or sets the text of a node and its descendants. Learn how setting it replaces children, how it differs from innerText and innerHTML, and when it returns null.
01
Kind
Read / write property
02
Returns
string or null
03
On set
Replaces children
04
vs innerText
No layout / CSS
05
vs innerHTML
Plain text safer
06
Status
Baseline widely
Fundamentals
Introduction
textContent is the go-to way to read or write plain text in the DOM. Reading concatenates all descendant text. Writing wipes the node’s children and puts one text node in their place.
It is often confused with innerText and innerHTML. Those APIs look similar but behave differently around styling, hidden content, and HTML parsing.
💡
Beginner tip
Prefer textContent when you only need plain text—especially for untrusted strings. Avoid innerHTML for text updates to reduce XSS risk.
Concept
Understanding textContent
MDN: textContent represents the text content of the node and its descendants. The exact result depends on the node type.
Element (and most other nodes) — concatenation of children’s text (comments excluded).
Setting — removes all children; inserts one text node.
Foundation
📝 Syntax
JavaScript
const text = element.textContent; // string (often)
element.textContent = "New plain text";
Return value
A string, or null for Document / DocumentType nodes.
Compare
⚖️ textContent vs innerText vs innerHTML
textContent
innerText
innerHTML
Returns
All text in subtree
Rendered / visible-oriented text
HTML markup string
Hidden elements
Included
Usually skipped
Markup still present
Layout cost
No reflow required
May trigger reflow
Parses HTML on set
XSS when setting
Treats input as text
Treats input as text
Can execute HTML / scripts risk
Best for
Plain text get/set
What users “see”
Trusted HTML fragments
Cheat Sheet
⚡ Quick Reference
Goal
Code
Read text
el.textContent
Set plain text
el.textContent = "Hello";
Whole page text
document.documentElement.textContent
Document itself
document.textContent → null
Single text node
textNode.nodeValue (same content)
MDN status
Baseline Widely available (since July 2015)
Snapshot
🔍 At a Glance
Four facts to remember about Node.textContent.
Returns
string | null
Text / null
Baseline
widely
Since July 2015
Access
get / set
Set clears kids
Prefer for
plain text
Safer than HTML
Hands-On
Examples Gallery
Examples follow MDN Node.textContent patterns. Use View Output or Try It Yourself for each case.
📚 Getting Started
Read concatenated text and replace children safely.
Example 1 — Read Nested Text (MDN-style)
Tags inside the element disappear from the string—only text remains.
JavaScript
// <div id="divA">This is <span>some</span> text!</div>
const text = document.getElementById("divA").textContent;
console.log(text); // "This is some text!"
const divA = document.getElementById("divA");
console.log(divA.children.length); // 1 (the span)
divA.textContent = "This text is different!";
console.log(divA.children.length); // 0
console.log(divA.textContent); // "This text is different!"
MDN advises against using innerHTML for plain text. textContent treats the string as text, which is the safer default for user input.
Example 4 — textContent Includes Hidden Text
innerText tends to skip hidden content; textContent does not.
JavaScript
const wrap = document.getElementById("wrap");
console.log(wrap.textContent.trim());
// includes "hidden" even if display:none
console.log(wrap.innerText.trim());
// often omits the hidden span's text
Node.textContent is Baseline Widely available across modern browsers (MDN: since July 2015). Logos use the shared browser-image-sprite.png sprite from this project.
✓ Baseline · Widely available
Node.textContent
Safe for production. Prefer textContent for plain text; remember setting it replaces all children.
UniversalWidely available
Google ChromeFull support · Desktop & Mobile
Full support
Mozilla FirefoxFull support · Desktop & Mobile
Full support
Apple SafariFull support · macOS & iOS
Full support
Microsoft EdgeFull support · Chromium & Legacy
Full support
OperaFull support · Modern versions
Full support
Internet ExplorerLong-standing support in legacy IE
Full support
textContentExcellent
Bottom line: Use textContent for plain text get/set; use innerText for visible text; use innerHTML only for trusted HTML.
Wrap Up
Conclusion
Node.textContent reads or writes plain text for a node and its descendants. Prefer it over innerHTML for text, know that setting clears children, and use document.documentElement.textContent when the Document itself returns null.
Use documentElement.textContent for full-page text
Choose innerText only when visibility matters
Pair with nodeValue for single text nodes
❌ Don’t
Use innerHTML for untrusted text
Assume document.textContent is a string
Confuse textContent with styled innerText
Set text when you meant to keep child elements
Confuse DOM Node with the Node.js runtime
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about textContent
Plain text of a node and its descendants.
5
Core concepts
📄01
string | null
get / set
API
🗑️02
Set clears kids
one text node
Gotcha
⚖️03
vs innerText
no CSS / reflow
Compare
🛡️04
Safer than HTML
prefer over innerHTML
Security
📄05
document null
use documentElement
Edge
❓ 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.
Did you know?
MDN notes that reading innerText can force a reflow because it depends on computed styles. Heavy loops that only need raw text should prefer textContent to avoid that cost.