diff --git a/blog_posts/js-textcontent-or-innertext.md b/blog_posts/js-textcontent-or-innertext.md new file mode 100644 index 000000000..eefee124f --- /dev/null +++ b/blog_posts/js-textcontent-or-innertext.md @@ -0,0 +1,97 @@ +--- +title: How are HTMLElement.innerText and Node.textContent different? +shortTitle: HTMLElement.innerText vs Node.textContent +type: story +tags: javascript,browser +author: chalarangelo +cover: blog_images/dark-city.jpg +excerpt: While these two properties are very similar, there are some key differences that you should be aware of. +firstSeen: 2023-03-19T05:00:00-04:00 +--- + +JavaScript provides two properties you can use to **access the text content of an element**: `Node.textContent` and `HTMLElement.innerText`. For the most part, these two appear to be interchangeable. In fact, many developers use them interchangeably, not knowing that there are important differences between the two. + +### Similarities + +I think it's helpful to identify the similarities of these two properties before diving into the differences. This will also clarify how they're used in most cases. + +Suppose you have an HTML element, containing some text: + +```html +

Hi there! My name is Bubbles.

+``` + +Both of these properties will return the text content of the element, including the text content of any **child elements**. They will also **ignore any HTML tags** that may be present in the element's content. And, they can be used to **set the text content** of the element, too. + +```js +const greeting = document.getElementById('greeting'); + +greeting.innerText; // "Hi there! My name is Bubbles." +greeting.textContent; // "Hi there! My name is Bubbles." + +greeting.innerText = 'Hello!'; //

Hello!

+greeting.textContent = 'Hi!'; //

Hi!

+``` + +### Differences + +So far, these two properties appear to do the exact same thing. In fact, they both offer some convenient features that make them very useful. However, they start to exhibit some differences when the element's content is a little more complex. + +Take the following HTML element, for example: + +```html +
+ +

Hi there!
My name is Bubbles.

+ And I'm a dog. +
+``` + +Let's take a look at the output of each of these two properties and see how they differ. + +```js +const card = document.querySelector('.card'); + +card.innerText; +/* +"Hi there! +My name is BUBBLES." +*/ + +card.textContent; +/* +" + + p { color: red; } + strong { text-transform: uppercase; } + small { display: none; } + + Hi there!My name is Bubbles. + And I'm a dog. +" +*/ +``` + +It's drastically different in this case, right? `HTMLElement.innerText` is supposed to roughly **match what the user sees** in the browser. Another way to think of this is that its output should closely resemble what the user would get if they were to select the element's content and copy it to their clipboard. + +The first thing to notice, based on this definition, is that **hidden elements are ignored**. This applies to elements that don't render, such as `