DEV Community

Ashiqur Rahman
Ashiqur Rahman

Posted on

5 3

Difference between innerText, textContent, innerHtml and outerHtml

Often we need to extract the text from Html elements. but
sometimes we are puzzled about the difference between them. let's tackle them now. this is our html element:



<h2>
   Sub Div
   <span>I am hidden</span>
</h2>


Enter fullscreen mode Exit fullscreen mode

we want to, extract the text part of the above element. let's write some javascript(basically dom-manipulation),



const heading = document.querySelector('h2');
console.log(heading.textContent);
console.log(heading.innerText);


Enter fullscreen mode Exit fullscreen mode

You can see in the browser console. both outputs are almost the same (the one which uses 'textContent' is not trimmed properly)

screenshot

So, what is the difference between them? For example, we add a style tag inside h2 element.



<h2>
   Sub Div
   <style>
      h2 {
      background-color: orangered;
      }
   </style>
   <span>I am hidden</span>
</h2>


Enter fullscreen mode Exit fullscreen mode

now if you console.log again. you will get this as output

screenshot-1

We are seeing that the textContent is giving the whole style element along with text and innerText is giving us the human-readable text.

There is another useful difference between them. suppose we hidden the visibility of an element by applying css rules and then try to extract them.



<head>
   <style>
      h2 span {
      display: none;
      }
   </style>
   <head>
<body>
   <h2>
      Sub Div
      <span>I am hidden</span>
   </h2>
</body>


Enter fullscreen mode Exit fullscreen mode

Now let's take a look what console.log gives us:

screenshot-2

We are seeing that textContent does not care about the css rules but innerText does. So, textContent would be handy If we need to get the text of an element that is hidden by CSS. 😉

The other two techniques are innerHtml and outerHtml. I think if we see the output in browser console we will understand them easily as they are pretty self-explanatory.



const heading = document.querySelector('h2');
console.log(heading.innerHTML);
console.log(heading.outerHTML);


Enter fullscreen mode Exit fullscreen mode

let's see the output in the browser cosnole

screenshot-4

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (1)

Collapse
 
naimur profile image
Naimur Rahman

I was using textContent most of the time to extract. But didn't know this shouldn't be used everywhere. Thanks

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay