HTML DOM Element innerText attribute
- Previous Page innerHTML
- Next Page insertAdjacentElement()
- Go to the Previous Level HTML DOM Elements Object
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:
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 = textor
node.innerText = textAttribute 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;
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 |
- Previous Page innerHTML
- Next Page insertAdjacentElement()
- Go to the Previous Level HTML DOM Elements Object