HTML DOM Element innerText attribute

Definition and Usage

innerText The attribute sets or returns the text content of the element.

Note:Set innerText When the attribute is set, all child nodes will be deleted and replaced by a single new text node.

See also:

textContent attribute

innerHTML property

Example

Get the inner text of an element:

let text = element.innerText;

Try it yourself

Syntax

Return the text content of an element or node:

element.innerText

or

node.innerText

Set the text content of an element or node:

element.innerText = text

or

node.innerText = text

Attribute Value

Value Description
text The text content of the element.

Return Value

Type Description
String Text content of the element and all its descendants, except for <script> and <style> elements.

The difference between innerHTML, innerText, and textContent

The innerText property returns:

Returns only the text content of the element and all its child elements, without CSS hidden text spacing and tags, except for <script> and <style> elements.

The innerHTML property returns:

Text content of the element, including all whitespace and internal HTML tags.

The textContent property returns:

Text content of the element and all its descendants, including whitespace and CSS hidden text, but without tags.

HTML Example

<p id="myP">   This element has extra spacing     and contains <span>a span element</span>.</p>

JavaScript Examples

let text = document.getElementById("myP").innerText;
let text = document.getElementById("myP").innerHTML;
let text = document.getElementById("demo").textContent;

Try it yourself

In the above example:

The innerText property returns:

This element has extra spacing and contains a span element.

The innerHTML property returns:

   This element has extra spacing and contains <span>a span element</span>.

The textContent property returns:

   This element has extra spacing and contains a span element.

Browser support

All browsers support element.innerText:

Chrome IE Edge Firefox Safari Opera
Chrome IE Edge Firefox Safari Opera
Support 10-11 Support Support Support Support