textContent property
The textContent property
allows you to retrieve and change
a text of an element.
Syntax
element.textContent;
Example
Let's output an element text:
<p id="elem">text</p>
let elem = document.querySelector('#elem');
console.log(elem.textContent);
The code execution result:
'text'
Example
Let's change an element text:
<p id="elem">text</p>
let elem = document.querySelector('#elem');
elem.textContent = '!!!';
The code execution result:
<p id="elem">!!!</p>
Example
If an element text contains tags, then when reading a property, these tags will not be displayed:
<p id="elem"><b>text</b></p>
let elem = document.querySelector('#elem');
console.log(elem.textContent);
The code execution result:
'text'