DOM Element.innerHTML Property
The innerHTML property gets or sets the HTML markup inside an element. Setting it with untrusted text can create security holes, so use textContent for plain text.
Description
The innerHTML property gets or sets the HTML markup of an element's contents. Reading it serializes the children to a string; setting it parses the string and replaces all children with the result, discarding the old elements and their event listeners.
Never set innerHTML with text that comes from users or other untrusted sources: any HTML in it is parsed, which can lead to cross-site scripting (XSS). Use textContent for plain text, or build elements with createElement(). To add HTML without replacing what's there, use insertAdjacentHTML().
Setting innerHTML inside a loop, as in el.innerHTML += item, re-parses everything on every pass and destroys existing listeners, so it's slow and error-prone. Building the whole string first, or using insertAdjacentHTML(), is better. Scripts inserted with innerHTML don't run, but inline event handlers in the markup do, which is why untrusted input is dangerous.
Syntax
element.innerHTML
element.innerHTML = htmlStringValue
A string of the HTML markup inside the element. Setting it replaces the element's contents.
Example
Browser Support
The following information will show you the current browser support for the Element.innerHTML property. Hover over a browser icon to see the version that first introduced support for this property.
Desktop





Tablets & Mobile





