Skip to content
Tutorials Center
My learning

Topics

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.

JavaScript
const html = list.innerHTML;       // read
list.innerHTML = "<li>One</li>";   // replace all children
list.innerHTML = "";               // remove all children

Try it

Read and replace markup
Why untrusted strings are dangerous

Here the attacker code is only a harmless console.log, but a real payload could read cookies or send data to another server.

Syntax

JavaScript
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 &amp;, omitted end tags are added, and so on. It is not the source text, and values typed into inputs are not included (only the value attribute 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. Use insertAdjacentHTML() or append() to add content.
  • Scripts. <script> elements inserted through innerHTML do not run. Event-handler attributes such as onerror and onclick do, 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 with textContent and attributes with setAttribute(). Browsers are also adding sanitizing APIs such as setHTML(); check a compatibility table before relying on them.
  • Related properties. outerHTML includes the element itself. On a <template> element, use its content property after setting innerHTML to 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.