JavaScript innerText property

The JavaScript innerText property is used to generate the dynamic text on the HTML document and is used to get the text content of the specified node, and all its descendants. The dynamic text can be a validation message, password strength, etc.

Syntax 1: To get the text content of a node

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
node.innerText
node.innerText
node.innerText

Syntax 2: To set the text content of a node

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
node.innerText = text
node.innerText = text
node.innerText = text

Example:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<!DOCTYPE html>
<html>
<body>
<button onclick="textreturn()" id="Button">CLICK ME</button>
<p id="DOM"></p>
<script>
function textreturn() {
var x = document.getElementById("Button").innerText;
document.getElementById("DOM").innerHTML = "The text of button is: " + x;
}
</script>
</body>
</html>
<!DOCTYPE html> <html> <body> <button onclick="textreturn()" id="Button">CLICK ME</button> <p id="DOM"></p> <script> function textreturn() { var x = document.getElementById("Button").innerText; document.getElementById("DOM").innerHTML = "The text of button is: " + x; } </script> </body> </html>
<!DOCTYPE html>
<html>
<body>
<button onclick="textreturn()" id="Button">CLICK ME</button>
<p id="DOM"></p>
<script>
function textreturn() {
var x = document.getElementById("Button").innerText;
document.getElementById("DOM").innerHTML = "The text of button is: " + x;
}
</script>
</body>
</html>