JavaScriptMedium
Difference between innerHTML and textContent?
textContent reads or sets an element's plain text (safe, fast, no HTML parsing); innerHTML reads or sets its HTML markup, which the browser parses into elements, so it is powerful but dangerous with untrusted input.
textContent
- Setting replaces all children with a single text node. Characters like
<are shown literally, never interpreted. - Reading returns the text of all descendants, including text hidden with CSS and inside
<script>/<style>. - Fast: no parsing, no layout needed.
innerHTML
- Setting parses the string as HTML and replaces the children. Any existing child elements are destroyed, which also removes their event listeners and state (like an input's typed value).
- Reading serialises the children back to an HTML string.
- XSS risk: inserting user-provided content can run attacker code, for example through
<img src=x onerror="...">. (Inserted<script>tags do not execute, but event-handler attributes do.)
innerText
Similar to textContent but layout-aware: it respects CSS (hidden text is excluded, line breaks follow the rendering). Reading it forces a layout, so it is slower.
Rules of thumb
- Displaying text, especially user content:
textContent. - Inserting trusted, static markup:
innerHTMLorinsertAdjacentHTMLis fine. - Inserting user content as HTML: sanitise first (DOMPurify, or the Sanitizer API's
setHTMLwhere available), or build elements withcreateElement. - Appending:
insertAdjacentHTML("beforeend", html)avoids re-parsing and destroying existing children, unlikeinnerHTML +=.
Code example
js
1document.body.innerHTML = `<div id="out"></div><p id="p">Hello <b>world</b><span style="display:none"> (hidden)</span></p>`;2const out = document.getElementById("out");3const userInput = '<img src="x" onerror="console.log(`XSS ran!`)"> Nice post';45// textContent: shown literally, never executed6out.textContent = userInput;7console.log(out.children.length, out.textContent.startsWith("<img")); // 0 true89// innerHTML parses markup -> the onerror handler would run with untrusted input!10// out.innerHTML = userInput; // don't do this with user content1112// Reading13const p = document.getElementById("p");14console.log(p.textContent); // "Hello world (hidden)" - all text15console.log(p.innerText); // "Hello world" - only what's rendered16console.log(p.innerHTML); // 'Hello <b>world</b><span style="display:none"> (hidden)</span>'1718// innerHTML += destroys and recreates existing children (losing listeners/state)19document.body.insertAdjacentHTML("beforeend", `<div id="box"><input id="name" value="typed"></div>`);20const box = document.getElementById("box");21const before = document.getElementById("name");22box.innerHTML += "<p>appended</p>";23console.log(before === document.getElementById("name")); // false - the input was recreated24box.insertAdjacentHTML("beforeend", "<p>appended safely</p>"); // keeps existing nodes
Practice this question
Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the JavaScript set.