The DOM innerText Property is used to set or return the text content of a specified node and its descendants. This property is very similar to the text content property but returns the content of all elements, except for <script> and <style> elements.Â
Syntax
It is used to set the innerText property.
node.innerText = text Return Value: It returns a string value that represents the text content of the element along with its descendants.Â
Example 1: In this example, we will see DOM innerText Property
<!--Driver Code Starts-->
<!DOCTYPE html>
<html>
<head>
<title>
HTML DOM innerText Property
</title>
</head>
<body>
<h1>GeeksforGeeks</h1>
<h2>HTML DOM textContent Property</h2>
<!--Driver Code Ends-->
<button id="geeks" onclick="MyGeeks()">
Submit
</button>
<p id="sudo"></p>
<script>
function MyGeeks() {
let text = document.getElementById("geeks").innerText;
document.getElementById("sudo").innerHTML = text;
}
</script>
<!--Driver Code Starts-->
</body>
</html>
<!--Driver Code Ends-->
Example 2: Â In this example, we will see DOM innerText Property
<!--Driver Code Starts-->
<!DOCTYPE html>
<html>
<head>
<title>HTML DOM innerText Property</title>
<style>
body {
text-align: center;
}
h1 {
color: green;
}
</style>
</head>
<body>
<h1>GeeksforGeeks</h1>
<h2>HTML DOM innerText Property</h2>
<p id="geeks" style="font-size: 20px;">
Hello Geeks!
</p>
<!--Driver Code Ends-->
<button onclick="MyGeeks()">
Submit
</button>
<script>
function MyGeeks() {
document.getElementById("geeks").innerText =
"Welcome to GeeksforGeeks!";
}
</script>
<!--Driver Code Starts-->
</body>
</html>
<!--Driver Code Ends-->
- innerText is style-aware and only includes âhuman-readableâ text.
- textContent returns raw text from all nodes (including <script> and <style>) and is not affected by CSS.