HTML DOM Element textContent
Examples
Return the text content of an element:
Change the textual content of a <p> element with id="demo":
Get all the textual content of an <ul> element with id="myList":
Description
The textContent property sets or returns the text content of the specified node,
and all its descendants.
Note
When you set the textContent property, all child nodes are removed and replaced by only one new text node.
See Also:
The Differences Between
innerHTML, innerText and textContent
See below
Syntax
Return the text content of a node:
Set the text content of a node:
Property Value
Return Value
Returns
null if the element is a document, a document type, or a notation.
The Differences Between
innerHTML, innerText and textContent
The text content of the element, including all spacing and inner HTML tags.
Just the text content of the element and all its children, without CSS hidden text spacing and tags, except <script> and <style> elements.
The text content of the element and all descendants, with spacing and CSS hidden text, but without tags.
HTML Example
JavaScript Examples
let text = document.getElementById("myP").innerHTML;
let text = document.getElementById("demo").textContent;
In the example above:
This element has extra spacing and contains a span element.
This element has extra spacing and contains <span>a span element</span>.
This element has extra spacing and contains a span element.
Browser Support
element.textContent is a DOM Level 3 (2004) feature.
It is fully supported in all modern browsers:
| Chrome | Edge | Firefox | Safari | Opera | IE |
| Yes | Yes | Yes | Yes | Yes | 11 |