The Wayback Machine - https://web.archive.org/web/20250116013645/https://www.geeksforgeeks.org/difference-between-textcontent-and-innerhtml/
Open In App

Difference between textContent and innerHTML

Last Updated : 07 Aug, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The textContent and innerHTML are properties of JavaScript. However, there are differences in the way the specified text is handled in JavaScript. Let us take a look at the syntax of both of these properties.

Syntax:

Let elem be a JavaScript variable that holds an element that is selected from the page.

let elem = document.getElementById('test-btn');

The textContent and innerHTML properties can be used as follows:

The textContent property: This property is used to get or set the text content of the specified node and its descendants.

elem.textContent

The innerHTML property: This property is used to get or set the HTML content of an element.

elem.innerHTML

Example: This example shows the difference between the innerHTML and textContent properties of DOM.

HTML
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" 
          content="width=device-width, 
                   initial-scale=1.0">
    <title>Document</title>
</head>

<body style="text-align:center;">
    <h1 style="color:#006600">
        GeeksforGeeks
    </h1>

    <div id="test-btn">
        The following element contains some
        <bold>bold</bold> and
        some <italic>italic text</italic>.
    </div>

    <p></p>

    <button onClick="innerHTMLfn()">
        innerHTML
    </button>

    <button onClick="textContentfn()">
        textContent
    </button>

    <p id="demo-para"></p>

    <script>
        function textContentfn() {
            let elem =
                document.getElementById('test-btn');
            alert(elem.textContent);
        }

        function innerHTMLfn() {
            let elem =
                document.getElementById('test-btn');
            alert(elem.innerHTML);
        } 
    </script>
</body>

</html>

Output:

The following video represents the difference between innerHTML and textContent. The appropriate results can be viewed by clicking on the respective button as shown.

Difference Table:

innerHTML

textContent

The innerHTML property gets or sets the HTML contents of the elementThe textContent does not automatically encode and decode text and hence allows us to work with only the content part of the element

Become an expert in solving problems with DSA JavaScript—the course designed to teach you Data Structures and Algorithms using JavaScript. Over the next 90 days, dive deep into key DSA concepts, learn to optimize your code, and gain hands-on experience solving challenging problems. Whether you're a beginner or looking to refine your JavaScript skills, this course will provide the knowledge you need to succeed.
Take on the Three 90 Challenge today! Complete 90% of the course within 90 days, and you’ll earn a 90% refund. This challenge is your chance to stay motivated, improve your skills, and get rewarded for your progress. Don’t wait—start your journey to DSA mastery with JavaScript today!


Next Article

Similar Reads

three90RightbarBannerImg