JavaScript Node textContent Property

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

What Is textContent?

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
GoalPrefer
Get / set plain textel.textContent
Visible text (CSS-aware)innerText
HTML markupinnerHTML
Single text / comment nodenodeValue

How to Use textContent

Read the string, or assign to replace the node’s children with text.

  1. Select an element (or other node).
  2. Read el.textContent for concatenated descendant text.
  3. Assign a string to set plain text (children are replaced).
  4. Use document.documentElement.textContent for whole-page text — not document.textContent.
PieceTypeRole
nodeNodeNode to read or update
Getstring or nullSubtree text (or null for Document / doctype)
SetstringReplace 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

Cheat Sheet

Keep these text facts handy.

TopicDetail
Syntaxnode.textContent / node.textContent = s
Get returnsstring or null
Set effectWipes children → one text node (MDN)
vs innerTextIgnores CSS; includes hidden; no reflow
vs innerHTMLPlain text; safer for untrusted strings
Documentnull — use documentElement
StandardBaseline Widely available (since July 2015)

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…

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!"
Try It Yourself

How It Works

  1. Select the element. Nested markup is fine.
  2. Read textContent. Walks descendants for text.
  3. Tags are not included. Only the text nodes matter.
  4. 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!
Try It Yourself

How It Works

  1. Assign a string. Old children are removed (MDN warning).
  2. One text node remains. That is the new content.
  3. Nested spans are gone. Do not set if you need to keep markup.
  4. 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
Try It Yourself

How It Works

  1. Assign untrusted input. Treated as characters, not markup.
  2. No HTML parser. Angle brackets stay visible as text.
  3. No injected elements. querySelector("img") finds nothing.
  4. 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.

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.

100% Widely available
Google Chrome Yes (Baseline)
Yes
Microsoft Edge Yes (Baseline)
Yes
Mozilla Firefox Yes (Baseline)
Yes
Apple Safari Yes (Baseline)
Yes
Opera Yes (Baseline)
Yes
Internet Explorer Supported (legacy)
Yes
textContent Excellent

Bottom line: Everyday DOM API. See the MDN textContent reference.

Performance Notes

ApproachCostBest for
Read textContentWalks text nodesPlain text extraction
Set textContentCheap replaceUpdating labels / messages
Read innerTextMay reflow (MDN)Visible, styled text only
Set innerHTMLHTML parseTrusted markup only

Prefer textContent when you do not need CSS-aware text or HTML parsing — it avoids reflow and the HTML parser.

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.

Next: appendChild()

Add a node as the last child without replacing existing content.

appendChild tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful