innerText and innerHTML are both properties of JavaScript. However, there are differences in which the text is handled. Let us check the syntax of the two and then take an example to look at the differences.
Syntax:
Let us assume that we have a JavaScript variable called x.
var x = document.getElementById('test');
- innerText
x.innerText
- innerHTML
x.innerHTML
Here’s how we can use innerText and innerHTML on this variable.
Example:
<!DOCTYPE html> <html> <head> <title> Difference between InnerHTML and InnerText </title> </head> <body style="text-align:center;"> <h1 style="color:green">GeeksforGeeks</h1> <div id="test"> The following element contains some <codes>code</codes> and <italic>some text</italic>. </div> <p></p> <button onClick="innerTextFn()"> innerHTML </button> <button onClick="innerHTMLFn()"> innerText </button> <p id="op"></p> <script> function innerTextFn() { var x = document.getElementById('test'); alert(x.innerText); } function innerHTMLFn() { var x = document.getElementById('test'); alert(x.innerHTML); } </script> </body> </html> |
Output
Before click:

After click innerHTML:

After click innerText:

Differences:
As we can see from the example above the innerText property sets or returns the text content as plain text of the specified node, and all its descendants whereas the innerHTML property gets and sets the plain text or HTML contents in the elements. Unlike innerText, inner HTML lets you work with HTML rich text and doesn’t automatically encode and decode text.
Recommended Posts:
- HTML | DOM innerText Property
- HTML | DOM innerHTML Property
- How to replace innerHTML of a div using jQuery?
- How to add innerHTML to print page?
- What is the Disadvantage of using innerHTML in JavaScript ?
- Difference between node.js require and ES6 import and export
- Difference Between DOS and Windows
- Difference Between Apache Kafka and Apache Flume
- Difference between Bootstrap and AngularJS
- Difference Between HTML and ASP
- Difference between ASP and ASP.NET
- Difference between Preemptive Priority based and Non-preemptive Priority based CPU scheduling algorithms
- What is the Difference between Website and Web Portal
- Difference between Web Scraping and Web Crawling
- Difference between TypeScript and JavaScript
- Difference between try-catch and if-else statements in PHP
- Difference between Algorithm, Pseudocode and Program
- Difference between Dropbox and OneDrive
- Difference between Applets and Servlets
- Difference between fundamental data types and derived data types
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


