In JavaScript, the innerHTML
and innerText
properties are used to access or modify the content of HTML elements. While they may appear similar, they serve distinct purposes and have significant differences in their behavior and use cases.
The innerHTML
property retrieves or sets the HTML content of an element, including any nested tags and text. It treats the content as HTML markup and can parse and render tags accordingly.
- Includes HTML Tags: Can interpret and render nested HTML elements.
- Modifies the DOM: Changes to
innerHTML
replace the entire content of the element. - Potential Security Risks: Using
innerHTML
with user input can lead to Cross-Site Scripting (XSS) vulnerabilities. - Slower for Large Content: Re-rendering the DOM for changes can impact performance in large or complex HTML structures.
const container = document.getElementById("example");
container.innerHTML = "<strong>Hello, World!</strong>";
Result in the DOM:
<div id="example"><strong>Hello, World!</strong></div>
console.log(container.innerHTML); // Output: "<strong>Hello, World!</strong>"
- Returns the full HTML structure, including tags.
The innerText
property retrieves or sets the text content of an element as it is rendered to the user. It excludes any HTML tags and reflects only the visible text, ignoring hidden elements (e.g., elements with display: none
or visibility: hidden
).
- Text-Only Content: Ignores any HTML tags and only returns or sets plain text.
- Excludes Hidden Elements: Only considers elements that are visible on the page.
- No Security Risks: Does not interpret HTML tags, making it safe from XSS vulnerabilities.
- Faster for Simple Text: Does not involve DOM re-rendering for plain text manipulation.
const container = document.getElementById("example");
container.innerText = "<strong>Hello, World!</strong>";
Result in the DOM:
<div id="example"><strong>Hello, World!</strong></div>
- The
<strong>
tags are treated as plain text and escaped as<
and>
.
console.log(container.innerText); // Output: "<strong>Hello, World!</strong>"
- Returns only the visible text, ignoring any HTML structure.
Aspect | innerHTML |
innerText |
---|---|---|
Purpose | Retrieves or sets the HTML markup and content. | Retrieves or sets the visible text content only. |
Includes HTML Tags | Yes, includes all HTML tags. | No, excludes tags and shows plain text. |
Reflects Hidden Text | Yes, includes text from hidden elements. | No, ignores text from hidden elements. |
XSS Vulnerability | Yes, susceptible if user input is not sanitized. | No, as it does not interpret HTML. |
Performance | Can be slower for complex DOM manipulations. | Faster for handling plain text. |
- You need to dynamically insert or update HTML content.
- Example: Rendering user-generated posts or building a list with nested elements.
const list = ["Apple", "Banana", "Cherry"];
const container = document.getElementById("example");
container.innerHTML = `<ul>${list.map(item => `<li>${item}</li>`).join("")}</ul>`;
// Result: A list with <ul> and <li> elements.
- You need to work with plain, visible text content.
- Example: Updating or extracting the text from a paragraph or heading.
const heading = document.getElementById("title");
heading.innerText = "Welcome to JavaScript!";
innerHTML
: Use with caution, especially with user-provided data. Always sanitize input to prevent XSS attacks.innerText
: Safer for manipulating text as it does not interpret HTML.
const container = document.getElementById("example");
container.innerHTML = "<strong>Hello</strong>, <em>World</em>!";
// Access HTML and text content
console.log(container.innerHTML); // Output: "<strong>Hello</strong>, <em>World</em>!"
console.log(container.innerText); // Output: "Hello, World!"
Property | Description |
---|---|
innerHTML |
Use to manipulate or retrieve HTML content, including tags. |
innerText |
Use to handle plain, visible text content without tags. |
By understanding the differences and appropriate use cases for innerHTML
and innerText
, you can write more efficient and secure JavaScript code.