freecoding.school100% FREE · NO SIGNUP
The Old NetISSUE #33 of 60

innerHTML · setting markup as a string

Pixel PeteVSQuirks Mode
Pixel Pete saysinnerHTML reads or replaces an element’s markup as a string — powerful but risky.

el.innerHTML = "<b>hi</b>" parses the string and rebuilds the element's children — fast for big chunks. But injecting untrusted input via innerHTML is the classic XSS hole: a malicious string can run scripts. Use it only with trusted/escaped content; for plain text use textContent, and for dynamic UIs prefer creating nodes.

Power-ups you unlock

Quirks Mode attacks — common mistakes

Boss battleBuild a small list with innerHTML from a trusted array, then note why user input would be unsafe.

Example code

<!doctype html><html><head><meta charset="utf-8"></head>
<body style="background:#06040d;color:#e6e0ff;padding:20px"><div id="o"></div>
<script>const items=['a','b','c'];document.getElementById('o').innerHTML='<ul>'+items.map(i=>'<li>'+i+'</li>').join('')+'</ul>';</script></body></html>
▶ Open the interactive comic issue
‹ Modern Selecting · QuerySelector · QuerySelectorAllInnerText Vs TextContent ›