HTML DOM Element innerText
Example
Get the inner text of an element:
let text = element.innerText;
Try it Yourself »
More examples below.
Definition and Usage
The innerText
property sets or returns the text content of an element.
Note
When you set the innerText 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 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
Property Value
Property | Description |
text | The text content of the element. |
Return Value
Type | Description |
String | The text content of the element and all descendants, except for <script> and <style> elements. |
The Differences Between
innerHTML, innerText and textContent
The innerHTML property returns: The text content of the element, including all spacing and inner HTML tags. |
The innerText property returns: Just the text content of the element and all its children, without CSS hidden text spacing and tags, except <script> and <style> elements. |
The textContent property returns: The text content of the element and all descendaces, with spacing 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 example above:
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
element.innerText
is supported in all browsers:
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | 10-11 | Yes | Yes | Yes | Yes |