JavaScript Node textContent Property

Beginner
⏱️ 10 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Read / write

What You’ll Learn

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

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.

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).
  • Text / Comment / CDATA — same idea as nodeValue.
  • Document / DocumentType — returns null.
  • Setting — removes all children; inserts one text node.

📝 Syntax

JavaScript
const text = element.textContent; // string (often)

element.textContent = "New plain text";

Return value

A string, or null for Document / DocumentType nodes.

⚖️ textContent vs innerText vs innerHTML

textContentinnerTextinnerHTML
ReturnsAll text in subtreeRendered / visible-oriented textHTML markup string
Hidden elementsIncludedUsually skippedMarkup still present
Layout costNo reflow requiredMay trigger reflowParses HTML on set
XSS when settingTreats input as textTreats input as textCan execute HTML / scripts risk
Best forPlain text get/setWhat users “see”Trusted HTML fragments

⚡ Quick Reference

GoalCode
Read textel.textContent
Set plain textel.textContent = "Hello";
Whole page textdocument.documentElement.textContent
Document itselfdocument.textContentnull
Single text nodetextNode.nodeValue (same content)
MDN statusBaseline Widely available (since July 2015)

🔍 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

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

How It Works

Descendant text nodes are concatenated. The <span> markup is not part of the returned string.

Example 2 — Setting Replaces All Children

MDN warning: assignment removes existing children.

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

How It Works

After the assignment, the span is gone. Only a single text node remains inside divA.

📈 Comparisons & Document Edge Cases

Safer than HTML insertion, different from visible text, Document is null.

Example 3 — Prefer textContent Over innerHTML for Text

Angle brackets stay as text—they are not parsed as tags.

JavaScript
const box = document.getElementById("box");
const untrusted = "<img src=x onerror=alert(1)>";

box.textContent = untrusted;
console.log(box.children.length); // 0 (no parsed element)
console.log(box.textContent);     // literal string with < and >
Try It Yourself

How It Works

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

How It Works

innerText is style-aware and may reflow. Use it when you care about what is visually rendered; use textContent for the raw text tree.

Example 5 — Document Returns null

Use documentElement when you want the whole document’s text.

JavaScript
console.log(document.textContent); // null

const rootText = document.documentElement.textContent;
console.log(typeof rootText); // "string"
Try It Yourself

How It Works

Per MDN, Document and doctype nodes return null. Climb to document.documentElement (<html>) for page text.

🚀 Common Use Cases

  • UI labels — set button / heading / status text safely.
  • Read without markup — scrape plain text from a container.
  • Clear and replace — wipe nested structure with one assignment.
  • Avoid XSS — prefer over innerHTML for untrusted strings.
  • Pair with nodeValue — use nodeValue when you already hold a Text node.

🧠 How textContent Works

1

Identify the node kind

Document, text, element, comment, and so on.

Type
2

Read: gather text

Concatenate descendant text (or return this node’s value / null).

Get
3

Write: replace children

Clear the subtree; insert one text node with the new string.

Set
4

Your UI updates

Plain text is shown without parsing HTML.

📝 Notes

  • Baseline Widely available (MDN, since July 2015).
  • Not Deprecated, Experimental, or Non-standard — no status banner required.
  • Setting textContent destroys nested elements—plan for that.
  • Related: nodeValue, appendChild(), childNodes, JavaScript hub.

Universal Browser Support

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.

Universal Widely available
Google Chrome Full support · Desktop & Mobile
Full support
Mozilla Firefox Full support · Desktop & Mobile
Full support
Apple Safari Full support · macOS & iOS
Full support
Microsoft Edge Full support · Chromium & Legacy
Full support
Opera Full support · Modern versions
Full support
Internet Explorer Long-standing support in legacy IE
Full support
textContent Excellent

Bottom line: Use textContent for plain text get/set; use innerText for visible text; use innerHTML only for trusted HTML.

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.

Continue with appendChild(), nodeValue, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Use textContent for plain text updates
  • Expect nested elements to vanish on set
  • 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

Key Takeaways

Knowledge Unlocked

Five things to remember about textContent

Plain text of a node and its descendants.

5
Core concepts
🗑️ 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.

Next: appendChild()

Add a node as the last child of a parent in the DOM.

appendChild() →

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.

5 people found this page helpful