Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Difference between innerText and innerHTML

  • Difficulty Level : Easy
  • Last Updated : 19 Jun, 2019

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.

Let us assume that we have a JavaScript variable called x.

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

var x = document.getElementById('test');

    Here’s how we can use innerText and innerHTML on this variable.

  • innerText
  • innerHTML


<!DOCTYPE html>
        Difference between 
      InnerHTML and InnerText
<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>.
    <button onClick="innerTextFn()">
    <button onClick="innerHTMLFn()">
    <p id="op"></p>
        function innerTextFn() {
            var x = document.getElementById('test');
        function innerHTMLFn() {
            var x = document.getElementById('test');

Before click:

After click innerHTML:

After click innerText:

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.

My Personal Notes arrow_drop_up
Recommended Articles
Page :