DEV Community

Cover image for innerText vs textContent in 2023
Ismael Ramos ๐Ÿš€
Ismael Ramos ๐Ÿš€

Posted on • Updated on • Originally published at ismaelramos.dev

innerText vs textContent in 2023

Table of content


What do I need to use? innerText or textContent? Here Iยดm going to analyse the differences
between both, and some hidden abilities that you may not know.

The code is available in this stackblitz.

The basics

Giving this HTML:

<div id="container">
  <span style="display: none">HIDDEN SECTION</span>
  <p><style>p { font-weight: bold}</style> Paragraph with line <br>break, spaces     and bold style.</p>
  <script>console.log('this is a script tag')</script>
</div>
Enter fullscreen mode Exit fullscreen mode
var containerDiv = document.querySelector('#container');
var textNode = document.createTextNode('text');
Enter fullscreen mode Exit fullscreen mode

innerText

  1. It is used to get the rendered text of an element.
  2. Does not include hidden elements like styles or scripts. Requires layout to calculate the result.
  3. Not available for text nodes, only for HTMLElement objects.
  4. Evaluates breaking lines tags. <br> -> \n
  5. No spaces will be concatenated.
  6. More performance-heavy. Take it wisely. In general, users won't notice the difference.
  7. 98.61% supported. Can I use
console.log(containerDiv.innerText);
// result:
// Paragraph with line
// break and bold style.

console.log(textNode.innerText);
// result:
// undefined
Enter fullscreen mode Exit fullscreen mode

textContent

  1. It represents the full-text content of an element and its descendants.
  2. Includes hidden elements like styles or scripts.
  3. Available for all Node objects.
  4. Evaluates control characters like \n.
  5. Can prevent XSS attacks. More info in this dom spec.
  6. 98.91% supported. Can I use. Be careful with IE 6-8.
console.log(containerDiv.textContent);
// result:
// 
// HIDDEN SECTION
// 
// 
// p {
//   font-weight: bold;
// }
// 
// Paragraph with line break, spaces and bold style.
// 
// 
// console.log('this is a script tag');
// 

console.log(textNode.textContent);
// result:
// word1
// word2
Enter fullscreen mode Exit fullscreen mode

Other differences

Text-transform property

In this case, we are going to use text-transform and this is what happens:

<div id="transform" style="text-transform: uppercase;">text uppercase</div>
Enter fullscreen mode Exit fullscreen mode
var transformDiv = document.querySelector('#transform');

// innerText
console.log(transformDiv.innerText);
// result:
// TEXT UPPERCASE

// textContent
console.log(transformDiv.textContent);
// result:
// text uppercase
Enter fullscreen mode Exit fullscreen mode

Setter

And what happens if we set textContent to some random text? You will lose all children nodes!
In fact, this is a very simple and fast way to delete them.

<p id="setter">
  <style>span {font-weight: bold;}</style> basic <span>text</span>
</p>
Enter fullscreen mode Exit fullscreen mode
var setterDiv = document.querySelector('#setter');

console.log(setterDiv.textContent);
// result:
// span {font-weight: bold;} basic text

setterDiv.textContent = 'some other text';
console.log(setterDiv.textContent);
// result:
// some other text
Enter fullscreen mode Exit fullscreen mode

As you can see, we lose all nodes inside the container.

Conclusion

So if you are trying to simply get the text from an element you can use innerText.
Consider also the use of innerHTML, which lets you work with HTML rich text and doesn't automatically encode and decode text, depending on what are your needs. Otherwise, textContent is the way.

a happy mandalorian

I hope you have learned something new. If you think this might help other people, please hit the like button so that others can read it. โค๏ธ

If you have any thoughts or questions, feel free to leave a comment!

Top comments (4)

Collapse
 
colinkiama profile image
Colin Kiama

Learning about innerText a while ago was a game changer for me. It's great that you shared this article!

Collapse
 
ismaestro profile image
Ismael Ramos ๐Ÿš€

:) nice to hear that! Thanks for the comment!

Collapse
 
sksage profile image
Siddharth Kanojiya

Very Nice

Collapse
 
ismaestro profile image
Ismael Ramos ๐Ÿš€

Thanks!