textContent in javascript

textContent in javascript

textContent property is used to get or set text content.

Syntax
element.textContent

Example 1 - set text content

Try Yourself

Example 2 - get text content

Try Yourself

Difference between textContent and innerText

In innerText the text which are visible in an web page are accessed. The hidden content cannot be retrieved.

While in textContent all the text including the hidden contents can be accessed.

In the below example, the "CodePlay" text is hidden using css display property.

When innerText button is clicked, only the text "tutorial" will appear.

When textContent button is clicked, the text "tutorial" and "CodePlay" will appear.

Example

Try Yourself