textContent property

The textContent property allows you to retrieve and change a text of an element.

Syntax

javascriptelement.textContent;

Example

Let's output an element text:

html<p id="elem">text</p>
javascriptlet elem = document.querySelector('#elem');
console.log(elem.textContent);

The code execution result:

javascript'text'

Example

Let's change an element text:

html<p id="elem">text</p>
javascriptlet elem = document.querySelector('#elem');
elem.textContent = '!!!';

The code execution result:

html<p id="elem">!!!</p>

Example

If an element text contains tags, then when reading a property, these tags will not be displayed:

html<p id="elem"><b>text</b></p>
javascriptlet elem = document.querySelector('#elem');
console.log(elem.textContent);

The code execution result:

javascript'text'

See also

  • the innerHTML property
    that contains an HTML code of an element
  • the outerHTML property
    that contains an outer HTML code of an element