Here we can explore difference between innerHtml,innerText and textContent.
Example:
<!DOCTYPE html>
<html>
<body>
<h1>The Element Object</h1>
<h2>The Differences Between innerText, innerHTML and textContent</h2>
<p id="myP"> This element has extra spacing <a style="display: none">hi</a> and contains <span>a span element</span>.</p>
<button onclick="getinnerHTML()">Get innerHTML</button>
<button onclick="getinnerText()">Get innerText</button>
<button onclick="gettextContent()">Get textContent</button>
<pre id="demo"></pre>
<script>
function getinnerText() {
let text = document.getElementById("myP").innerText;
document.getElementById("demo").innerText = text;
}
function getinnerHTML() {
let text = document.getElementById("myP").innerHTML;
document.getElementById("demo").innerText = text;
}
function gettextContent() {
let text = document.getElementById("myP").textContent;
document.getElementById("demo").innerText = text;
}
</script>
</body>
</html>
innerHtml output:
innerHtml -print with tags,spacing everything whatever we given.
innerText output:
- innerText prints only content in elements with good alignment(without spacing)
- without tags and hiding display not shown
textContent output:
- textContent prints- with spacing ,without tags
- hiding display shown here.


Top comments (0)