JavaScript innerText vs. innerHTML and textContent
innerText reads rendered text, while innerHTML reads or replaces the HTML markup inside an element. Use textContent to show a plain string without parsing it as HTML. Assigning any of the three replaces the existing children of the element you target.
Read and write the properties
const element = document.getElementById("message");
if (element !== null) {
const visibleText = element.innerText;
const markup = element.innerHTML;
element.innerText = "Updated message";
}
These are properties, so access them without parentheses. Check for null if the element ID may be missing or if the script might run before the element exists. The Document Object Model (DOM) is the browser’s tree of elements and text nodes. Reading HTML gives you a serialization of the current tree, which the browser may normalize.
Choose between innerText, textContent, and innerHTML
| Property | Reads | Writes | Typical use |
|---|---|---|---|
innerText |
Text as rendered, including line breaks | Text with br elements for newlines | Read visible text |
textContent |
Text including hidden descendants | Plain text without parsing markup | Show strings and computed values |
innerHTML |
Child markup | Parses markup and replaces children | Insert controlled markup |
innerText generally excludes hidden descendants of an element that is being rendered. When the element itself is not being rendered, including when detached, the getter falls back to its text content. A read can require up-to-date layout. textContent ignores appearance and can include text from script and style elements.
Compare the same input in a complete example
Save the complete example as text-html.html in UTF-8 and open it. No library or server is required. The results are written into a pre using textContent, so you can inspect the actual string values.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Compare text and HTML</title>
</head>
<body>
<h1>Compare text and HTML</h1>
<div id="source">Hello <strong>HTML</strong><span hidden>Hidden sentence</span><br>Second line</div>
<pre id="read-result"></pre>
<p>Use the buttons to insert the same string in two ways.</p>
<button id="as-text" type="button">Insert as text</button>
<button id="as-html" type="button">Insert as HTML</button>
<button id="restore" type="button">Restore</button>
<div id="target">Initial content</div>
<script>
const source = document.getElementById("source");
const output = document.getElementById("read-result");
const target = document.getElementById("target");
output.textContent = [
`innerText: ${JSON.stringify(source.innerText)}`,
`textContent: ${JSON.stringify(source.textContent)}`,
`innerHTML: ${source.innerHTML}`
].join("\n");
// Use only this fixed markup from the example.
const markup = "<em>Emphasis</em>\nSecond line";
document.getElementById("as-text").addEventListener("click", () => {
target.innerText = markup;
});
document.getElementById("as-html").addEventListener("click", () => {
target.innerHTML = markup;
});
document.getElementById("restore").addEventListener("click", () => {
target.textContent = "Initial content";
});
</script>
</body>
</html>
The initial innerText separates Hello HTML and Second line with a line break. textContent includes Hidden sentence but does not insert a newline for br. innerHTML returns the tags. JSON.stringify() makes newline characters visible as \n.
Use Insert as text to see the literal <em> markup followed by a new line. Insert as HTML creates an em element. With normal HTML whitespace handling, the raw newline is ordinarily collapsed. Click Restore before repeating the comparison. If the buttons do nothing, check the Console and verify the file extension.
Display external input as text
Use innerHTML with controlled markup. Assigning untrusted strings from forms, URL parameters, or responses can introduce cross-site scripting (XSS). For labels and messages, write plain text. If you need emphasis, create a strong element and set its textContent. Allowing user-authored markup calls for a vetted sanitization policy.
const name = "<em>Example</em>";
const message = document.getElementById("message");
if (message !== null) {
message.textContent = name;
}
Preserve existing elements and event handlers
element.innerHTML += … serializes and reparses the existing content. Children can be replaced, so listeners registered directly on the old nodes and widget state can be lost. Assigning innerText or textContent to a parent also removes its children. Update the specific child that holds the label.
Update a button label without replacing the button
Save the code below as button-label.html and open it in a browser. Set textContent on the label’s span while keeping the button and its icon in place.
<!doctype html>
<html lang="en">
<meta charset="utf-8">
<title>Update a label without replacing its button</title>
<button type="button" id="save">
<span aria-hidden="true">★</span> <span id="save-label">Save</span>
</button>
<p id="status" role="status">Clicks: 0</p>
<script>
const button = document.getElementById("save");
const label = document.getElementById("save-label");
const status = document.getElementById("status");
let clicks = 0;
button.addEventListener("click", () => {
status.textContent = "Clicks: " + ++clicks;
});
label.textContent = "Save <draft>";
</script>
</html>
The icon appears next to the literal label Save <draft>. Click twice to get Clicks: 2. The button’s click listener still runs because the update did not replace the button or its icon.




