Favorites

DOM Element.innerHTML Property

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee
Element.innerHTML
DOM Element PropertyThis property is supported by all modern browsers.
Updated 28th November 2026

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 = htmlString

Value

A string of the HTML markup inside the element. Setting it replaces the element's contents.

Example

<div id="box"><p>Old <em>content</em></p></div>
const box = document.getElementById('box');
console.log(box.innerHTML);
box.innerHTML = '<ul><li>One</li><li>Two</li></ul>';
console.log(box.querySelectorAll('li').length);

const userInput = '<img src=x onerror="alert(1)">';
box.textContent = userInput;   // safe: shown as text

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.

This property is supported by all modern browsers.

Desktop

Chrome
Edge
Firefox
Opera
Safari

Tablets & Mobile

Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-
Supported Unknown Not supported
☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee