innerText vs. innerHTML vs. textContent.

Guillermo Cardoze
4 min readJan 26, 2024

--

JavaScript, being a versatile language, provides various ways to manipulate HTML content dynamically. When it comes to updating the content within HTML elements, developers often encounter three commonly used properties: innerHTML, innerText, and textContent. In this blog post, we will delve into each of these properties, exploring their differences, use cases, and best practices.

I have heard these terms in the coding world and still get stumped on which one to use for what application. Knowing what you want your application to show in the browser, will depend on InnerText, innerHTML, or textContent.

As we know that all three of these properties can manipulate the DOM, we still need to understand what is the difference.

Here is my understanding how they are used.

innerText — gives us just the text inside the tag as a human readable text. No HTML tags, hidden elements, line breaks, or spaces are shown in the browser.

EXAMPLE INNERTEXT // Getting the text content
const heading = document.getElementById(‘myHeading’);
const textContent = heading.innerText;
console.log(textContent);

// Setting the text content
heading.innerText = ‘New heading text’;

innerHTML- shows the HTML tags as well as the children nodes, within them. innerHTML is not NOT SECURE to use for it can leave a chance for the user to plant a bug, or malicious code, to your javascript and HTML.

EXAMPLE INNERHTML // Getting the HTML content
const container = document.getElementById(‘myContainer’);
const htmlContent = container.innerHTML;
console.log(htmlContent);

// Setting the HTML content
container.innerHTML = ‘<p>Hello, <strong>world</strong>!</p>’;

textContent- shows the text only without the HTML tags but still with the spaces. Think as if the HTML tags were invisible. The text would still show in the same place. Hidden elements are included as well as the text content of the node and its children nodes.

EXAMPLE TEXT CONTENT // Getting the text content
const paragraph = document.getElementById(‘myParagraph’);
const rawTextContent = paragraph.textContent;
console.log(rawTextContent);

// Setting the text content
paragraph.textContent = ‘New paragraph text’;

Choosing the Right Property

Now that we’ve explored these properties, the question arises: which one should you use? The choice depends on your specific use case and the requirements of your application.

  • Use innerHTML when you need to manipulate the HTML content within an element, including tags and attributes.
  • Use innerText when you want to work with the visible text content of an element, considering styles.
  • Use textContent when you need the raw text content of an element without considering styles.
  • Understanding innerHTML, innerText, and textContent in JavaScript
  • JavaScript, being a versatile language, provides various ways to manipulate HTML content dynamically. When it comes to updating the content within HTML elements, developers often encounter three commonly used properties: innerHTML, innerText, and textContent. In this blog post, we will dive into each of these properties, exploring their differences, use cases, and best practices.

innerHTML

The innerHTML property is a powerful tool for manipulating the HTML content inside an element. It allows you to get or set the HTML content, including tags and attributes. Let's take a look at some examples:

javascriptCopy code
// Getting the HTML content
const container = document.getElementById('myContainer');
const htmlContent = container.innerHTML;
console.log(htmlContent);
// Setting the HTML content
container.innerHTML = '<p>Hello, <strong>world</strong>!</p>';

In the above example, the innerHTML property is used to retrieve the HTML content of the element with the ID 'myContainer' and then set a new HTML content.

It’s essential to be cautious when using innerHTML, especially with user-generated content, as it can expose your application to potential security vulnerabilities like Cross-Site Scripting (XSS) attacks. Always validate and sanitize user inputs before using them in innerHTML.

innerText

The innerText property, on the other hand, deals with the text content inside an element, excluding HTML tags. It provides a way to access or modify the text content without considering the HTML structure. Here's an example:

// Getting the text content
const heading = document.getElementById('myHeading');
const textContent = heading.innerText;
console.log(textContent);
// Setting the text content
heading.innerText = 'New heading text';

In this example, innerText is used to retrieve the text content of an element and then update it with new text. It's worth noting that innerText considers the CSS styles applied to the element, potentially affecting the result.

textContent

Similar to innerText, the textContent property is used for working with the text content of an element. However, there are subtle differences between the two. Unlike innerText, textContent doesn't consider styles and provides the raw text content, including whitespace and line breaks. Here's how you can use it:

// Getting the text content
const paragraph = document.getElementById('myParagraph');
const rawTextContent = paragraph.textContent;
console.log(rawTextContent);

In the above example, textContent is used to get the raw text content of an element and then update it with new text. This property is often preferred over when you need the raw text without any styling considerations.

Best Practices and Considerations

Regardless of the property you choose, it’s essential to keep some best practices in mind:

  1. Sanitize User Inputs: When using innerHTML, always validate and sanitize user inputs to prevent potential security vulnerabilities.
  2. Consider Styles: Be aware that innerText and textContent may behave differently based on CSS styles applied to the elements.
  3. Performance: In terms of performance, textContent is often faster than innerText since it doesn't consider styles.
  4. Cross-Browser Compatibility: While these properties are widely supported, it’s good practice to test your code across different browsers to ensure compatibility.

--

--