JavaScript: innerHTML

The innerHTML property reads or replaces an element’s contents as HTML markup.

What you will learn

Minimal example

const box = document.querySelector('#box');
if (box) box.innerHTML = '<strong>Ready</strong>';

The <strong> element is parsed and rendered as bold text. If you only need to display a string, prefer textContent so the value is treated as plain text.

Security rule

Never place user-controlled or otherwise untrusted text directly into innerHTML. It can create a cross-site scripting (XSS) vulnerability. Build trusted markup carefully, or use DOM methods and textContent for plain text.

Common mistakes