JavaScript Reference ยท DOM: Document and Elements
Element.innerHTML
Reference for Element.innerHTML: read an element's markup, replace its children from a string, and avoid XSS, lost listeners and the innerHTML += trap.
The innerHTML property gets or sets the HTML markup inside an element. Reading it serializes the element's descendants into a string. Writing to it parses a string as HTML and replaces all existing children with the new nodes.
It is convenient for quick templates, but it is also the most common source of cross-site scripting (XSS) bugs in front-end code. Whenever any part of the string comes from a user, a URL or a server you do not control, use textContent or build elements with createElement() instead.
const html = list.innerHTML; // read list.innerHTML = "<li>One</li>"; // replace all children list.innerHTML = ""; // remove all children
Try it
Here the attacker code is only a harmless console.log, but a real payload could read cookies or send data to another server.
Syntax
const markup = element.innerHTML; element.innerHTML = markup;
| Name | Description |
|---|---|
markup |
A string of HTML. When setting, it is parsed in the context of element. Assigning an empty string removes all children. |
Return value (getter): a string with the serialized HTML of the element's children.
Exceptions: setting can throw SyntaxError for XML documents when the string is not well-formed. In HTML documents the parser is forgiving and does not throw.
Usage notes
- The getter returns normalized HTML. The browser re-serializes the live DOM, so attribute quotes become double quotes,
&becomes&, omitted end tags are added, and so on. It is not the source text, and values typed into inputs are not included (only thevalueattribute is). - Setting replaces everything. Old child nodes are removed and new ones are created. References to old nodes now point to detached elements, and their event listeners, focus, scroll position and form state are gone.
- Avoid
el.innerHTML += more. It serializes the whole subtree, appends the string and parses everything again. UseinsertAdjacentHTML()orappend()to add content. - Scripts.
<script>elements inserted throughinnerHTMLdo not run. Event-handler attributes such asonerrorandonclickdo, so removing<script>tags is not a defence. - Escape or avoid user data. If you must build markup from data, escape
&,<,>,"and', or better, set text withtextContentand attributes withsetAttribute(). Browsers are also adding sanitizing APIs such assetHTML(); check a compatibility table before relying on them. - Related properties.
outerHTMLincludes the element itself. On a<template>element, use itscontentproperty after settinginnerHTMLto get a document fragment. - Performance. One assignment with a whole string is much faster than inserting many nodes one at a time in a loop. For repeated updates of large lists, consider building a
DocumentFragment.
Browser support
Supported in every browser.
Related
Node.textContentโ safe text getter and setterHTMLElement.innerTextโ rendered textElement.insertAdjacentHTML()โ add markup without replacing childrendocument.createElement()โ build nodes without parsing strings- Lesson: Creating and changing elements