CKTIS / JavaScript
Element.innerHTML — read and replace HTML contents
Element.innerHTML reads an element’s contents as HTML markup or replaces them with nodes parsed from supplied markup. It does not sanitize untrusted data and can introduce XSS.
Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support
Syntax
const markup = element.innerHTML;
element.innerHTML = markup;Element.innerHTML is a Web API property, not part of ECMAScript itself. Reading it serializes the element’s contents without its outer tag and without shadow DOM trees. The result need not match the original source text exactly.
Assignment parses markup in the element’s context and replaces its previous contents; it does not replace the element itself. For template, it changes the template contents. In an XML document, malformed XML causes a SyntaxError.
Security: assignment does not remove dangerous elements or attributes by default. The fact that inserted script elements do not execute does not prevent XSS, for example through event-handler attributes. Do not pass untrusted HTML directly. When Trusted Types are enforced, assigning an ordinary string may be rejected.
The example uses only fixed, trusted markup. The second assignment replaces the first contents, leaving one child. Run it in a browser; the presence of Node 24 in a repository does not establish support for this API.
Examples
const box = document.createElement('div');
box.innerHTML = '<strong>Hello</strong>';
console.log(box.innerHTML);
box.innerHTML = '<em>Bye</em>';
console.log(box.innerHTML);
console.log(box.children.length);Result
<strong>Hello</strong> <em>Bye</em> 1
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.