English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
innerTextAttribute to set or return the text content of a specified node.
The innerText property can be used to write dynamic text to an HTML document. Here, the text is not interpreted as HTML text, but as plain text.
If the innerText property is set, any child nodes will be removed and replaced by a single text node containing the specified string.
This property is similar totextContentattribute, but textContent returns the text content of all elements, while innerText returns the content of all elements except <script> and <style> elements.
To set or return the HTML content of an element, useinnerHTMLAttribute.
Return text content:
HTMLElement.innerText
Set text content:
HTMLElement.innerText = text
document.getElementById("para").innerText = "Hello world";Test to see‹/›
The numbers in the table specify the first browser version that fully supports the innerText attribute:
Attribute | |||||
innerText | 4 | 45 | 10.5 | 3 | 10 |
Value | Description |
---|---|
text | Specify the text content of an element |
Return value: | A string representing the rendered text content of an element |
---|---|
DOM Version: | DOM Level1 |
This example demonstrates the difference between innerText, innerHTML, and textContent:
<p id="x">This element has additional spacing and contains a <span>span</span> element</p>/span>.</p> <script> function getInnerText() {}} alert(document.getElementById("x").innerText); } function getInnerHTML() { alert(document.getElementById("x").innerHTML); } function getTextContent() { alert(document.getElementById("x").textContent); } </script>Test to see‹/›
The innerText property only returns text, without spaces and internal element tags.
The innerHTML property returns text with spaces and internal element tags.
The textContent property returns text with spaces, but without internal element tags.
HTML DOM Reference:HTML DOM innerHTML property
HTML DOM Reference:HTML DOM textContent property