HTML Element .innerText Since: HTML5(IE origin → standardized)
A property that gets or sets the visible text of an HTML element. Text hidden via CSS is not included, so it only reflects what is actually displayed on screen.
Syntax
// Get the visible text. var text = element.innerText; // Set the text. element.innerText = "New text";
Sample Code
<div id="box"> <p>Visible text</p> <p style="display: none;">Hidden text</p> <span>Another text</span> </div>
var box = document.querySelector("#box");
// innerText only retrieves text that is currently visible on screen.
console.log(box.innerText);
// Returns "Visible text" and "Another text".
// Hidden text is not included.
// textContent retrieves all text regardless of CSS visibility.
console.log(box.textContent);
// Returns "Visible text", "Hidden text", and "Another text".
// Setting the text replaces all child elements.
box.innerText = "New text";
Difference Between innerText and textContent
| Property | Description |
|---|---|
| innerText | Returns only the text that is visible on screen. Text hidden via CSS is excluded, and line breaks reflect <br> tags and block element boundaries. Triggers a layout recalculation, which may affect performance. |
| textContent | Returns the content of all text nodes regardless of CSS visibility. Whitespace and line breaks are preserved as they appear in the HTML source. Does not trigger a layout recalculation, so it is faster. |
Overview
element.innerText is a property that gets or sets the visible text of an HTML element. It is similar to element.textContent, but unlike textContent, innerText excludes text from elements hidden via CSS and returns only what is actually rendered on screen.
element.innerText accesses layout information, causing the browser to recalculate the layout each time it is called. Calling it repeatedly on many elements inside a loop may degrade performance. If you do not need to account for visibility, using element.textContent is more efficient.
When setting text, both element.innerText and element.textContent replace all existing child elements with a text node. HTML tags are treated as literal strings and are not parsed as markup. If you need the value to be interpreted as HTML, use element.innerHTML instead.
Browser Compatibility
44 or earlier ×
4.5 or earlier ×
9 or earlier ×
Android Browser
37+ ○
Chrome Android
36+ ○
17 or earlier ×
Firefox Android
79+ ○
44 or earlier ×If you find any errors or copyright issues, please contact us.