Skip to content

Topics

Jump to a topic.

Search questions

Find a question by title, tag, or summary.

How do innerHTML, textContent, innerText, and setHTML() differ?

Questions in this chapter (6)
  • Draft
  • medium
  • Askedvery often
  • Verified October 2026

Answer

innerHTML. The getter serialises the element's children as HTML. The setter runs the HTML fragment parser with the element as context and replaces all children with the result. Two consequences:

  • <script> elements it creates never execute: the fragment is parsed in an inert scripting mode.
  • Everything else is live markup. <img src=x onerror=…> runs its handler when the image fails, so assigning untrusted strings is an XSS hole.

textContent. Defined by the DOM Standard on every node. On an element it returns the concatenated data of all descendant Text nodes in tree order: including text in <script>, <style>, and elements hidden by CSS or hidden. On document it's null. The setter replaces all children with one Text node (none for ""), so markup is never parsed. It's the safe, cheap way to write text.

innerText. Defined by the HTML Standard on HTMLElement, it returns the text as rendered: elements with display: none or non-visible visibility are skipped, text-transform is applied, and block boundaries and <br> become line breaks. That needs up-to-date styles and layout, so reading it after DOM changes forces a synchronous layout. If the element itself isn't rendered, it falls back to textContent-style output. The setter turns \n into <br>.

setHTML(html, { sanitizer }). Parses like innerHTML, then sanitises before inserting. With no sanitizer it uses the built-in safe default, which keeps only elements categorised Default (<b>, <p>… but not <img>), and drops comments, data-* attributes and javascript: URLs. With a custom config it still removes everything unsafe, such as <script> and on* handlers, whatever the config allows. setHTMLUnsafe() is the unsanitised counterpart that, unlike innerHTML, parses declarative shadow roots. The Sanitizer API moved from its WICG draft into the HTML Standard; setHTML() shipped in Chrome 146 and Firefox 148, not in Safari.

Example

// <div id="box"><p>Hello <span hidden>secret</span>
//   <span style="text-transform: uppercase">world</span></p></div>   (no whitespace between tags)
// <div id="out"></div>
const box = document.getElementById('box');
console.log(JSON.stringify(box.textContent));
// → "Hello secretworld"
console.log(JSON.stringify(box.innerText));
// → "Hello WORLD"

const out = document.getElementById('out');
const payload = '<img src="x" onerror="console.log(\'onerror ran\')">' +
  '<script>console.log("script ran")<\/script><b>ok</b>';

out.setHTML(payload);
console.log(out.innerHTML);
// → <b>ok</b>
out.setHTML(payload, { sanitizer: { elements: ['img', 'script', 'b'], attributes: ['src', 'onerror'] } });
console.log(out.innerHTML);
// → <img src="x"><b>ok</b>
out.textContent = payload;
console.log(out.childNodes.length, out.firstChild.nodeName);
// → 1 #text
out.innerHTML = payload;
// The script never runs; the image fails and its handler logs:
// → onerror ran

Chrome 156 and Firefox 159 printed the same.

Pitfalls

  • Using innerHTML to insert text. Any < in user data becomes markup; use textContent.
  • Expecting a <script> added with innerHTML to run. It won't; create it with createElement('script') and insert it.
  • Reading innerText in a loop that also writes to the DOM. Each read needs fresh layout; read textContent unless you need the rendered text.
  • Trusting setHTML() with a permissive config to keep event handlers. Unsafe content is always removed; use setHTMLUnsafe() only for trusted markup.

Follow-ups

Does el.innerHTML += '<li>x</li>' just append?
No. It serialises all children, concatenates, and re-parses: every existing child is replaced by a new node, losing listeners, form state and references. Use insertAdjacentHTML('beforeend', …) or append().
What does innerText return on an element with display: none?
Its descendant text content, as textContent would, because the "not being rendered" case falls back to it. A hidden child of a rendered element is skipped instead.

Exercises

What do the three logs print?

// <ul id="menu"><li>Home</li><li style="display: none">Admin</li><li>About</li></ul>
const menu = document.getElementById('menu');
console.log(JSON.stringify(menu.textContent));
console.log(JSON.stringify(menu.innerText));
menu.style.display = 'none';
console.log(JSON.stringify(menu.innerText));

Comments

Comments are GitHub Discussions: you sign in with GitHub to post or react.