Thursday, November 20, 2025
HomeLanguagesJavascriptDifference between innerText and innerHTML

Difference between innerText and innerHTML

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.

let x = document.getElementById('test');
  • innerText
x.innerText
  • innerHTML
x.innerHTML

Example: 

html




<!DOCTYPE html>
<html>
<head>
    <title>
        Difference between
        InnerHTML and InnerText
    </title>
</head>
 
<body style="text-align:center;">
 
    <h1 style="color:green">neveropen</h1>
 
    <div id="test">
        The following element contains some
        <codes>code</codes> and
        <italic>some text</italic>.
    </div>
    <p></p>
    <button onClick="innerHTMLFn()">
        innerHTML
    </button>
    <button onClick="innerTextFn()">
        innerText
    </button>
    <p id="op"></p>
 
    <script>
        function innerTextFn() {
            let x = document.getElementById('test');
            alert(x.innerText);
        }
 
        function innerHTMLFn() {
            let x = document.getElementById('test');
            alert(x.innerHTML);
        }
    </script>
</body>
</html>


Output 

 

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.

RELATED ARTICLES

Most Popular

Dominic
32404 POSTS0 COMMENTS
Milvus
97 POSTS0 COMMENTS
Nango Kala
6776 POSTS0 COMMENTS
Nicole Veronica
11925 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11994 POSTS0 COMMENTS
Shaida Kate Naidoo
6905 POSTS0 COMMENTS
Ted Musemwa
7160 POSTS0 COMMENTS
Thapelo Manthata
6861 POSTS0 COMMENTS
Umr Jansen
6846 POSTS0 COMMENTS