Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

HTMLElement.innerHTML: read and replace an element’s contents

Reading `innerHTML` serializes an element’s contents as markup. Assigning to it replaces the contents with parsed nodes, even when the assignment uses `+=`.

Version: ECMAScript 2024 and the current web platform; runtime support varies

Syntax

const markup = element.innerHTML;
element.innerHTML = value; // string or TrustedHTML; null is treated as ""

HTMLElement.innerHTML is an instance property inherited from Element, not a static method. Reading it returns the current DOM contents as a string, without the element’s own tags or any shadow roots. It serializes the tree; it does not recover the original source text.

Assignment parses markup in the element’s context and replaces its child nodes, not the element itself. For template, it replaces the contents of template.content. The setter accepts a string or TrustedHTML. Web IDL converts null to the empty string ("") before the Trusted Types check. If that string reaches the parser unchanged, it clears the contents. Under Trusted Types enforcement, however, a default policy can turn the empty string into nonempty markup, so a successful assignment does not necessarily clear the contents. The setter does not create declarative shadow roots.

element.innerHTML += markup reads the serialized markup, concatenates the new text, and reparses the whole result. The old nodes are detached, and listeners registered directly on them do not transfer to their replacements. To preserve existing nodes, add new ones with append().

Do not pass untrusted input directly to innerHTML: it does not sanitize by default, and event-handler attributes can lead to XSS even though inserted script elements do not execute. Use textContent for plain text. If you need HTML, use a vetted sanitizer. A Trusted Types policy does not by itself guarantee sanitization.

This is a web platform API, not part of ECMAScript 2024. Trusted Types support varies by environment; when CSP enforces Trusted Types and no default policy exists, assigning a string throws TypeError. HTML parsing recovers from syntax errors, but in an XML document a malformed fragment can throw SyntaxError on assignment, and contents that cannot be serialized as well-formed XML can throw InvalidStateError on read.

Examples

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>innerHTML example</title>
</head>
<body>
  <script>
    const box = document.createElement("div");
    box.innerHTML = "<strong>Hello</strong>";
    const original = box.firstElementChild;

    box.innerHTML += "<em> again</em>";

    console.log(box.innerHTML);
    console.log(box.textContent);
    console.log(original === box.firstElementChild);
    console.log(original.parentNode === null);
  </script>
</body>
</html>

Result

<strong>Hello</strong><em> again</em>
Hello again
false
true

Save the example as an HTML file, open it in a browser without Trusted Types enforcement, and check the developer console. The first two lines contrast markup with text; the last two show that the first node was replaced and no longer has a parent.

Related lessons

  • JavaScript · Lesson 31 · Advanced · Version: 4

Sources