Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Node.textContent — reading and replacing text

Node.textContent reads a node’s text or replaces its textual content without parsing HTML.

Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support

Syntax

node.textContent
node.textContent = text

Node.textContent is a DOM property, not an ECMAScript feature. For Element and DocumentFragment, it concatenates descendant Text data in tree order, without separators, excluding comments. It does not filter text by CSS visibility.

Assignment replaces all children with one Text node; '' or null removes them without creating one. Markup remains text. For Attr, the property reads or changes the value; for CharacterData, the data. For Document and DocumentType, reading returns null and writing does nothing.

Examples

const box = document.createElement('div');
const child = document.createElement('span');
child.textContent = 'world';
box.append('Hello ', child);
console.log(box.textContent);
box.textContent = '<b>Literal</b>';
console.log(box.textContent);
console.log(box.childNodes.length, box.firstChild.nodeType);
box.textContent = null;
console.log(box.childNodes.length);

Result

Hello world
<b>Literal</b>
1 3
0

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources