Element.innerHTML is a read/write instance property that gets or sets the HTML markup inside an element. Learn how to read and replace contents safely, when to prefer textContent, and how XSS risks appear—with five examples and try-it labs.
01
Kind
Instance property
02
Access
Get and set
03
Type
string
04
Effect
Replace children
05
Status
Baseline widely
06
Caution
XSS / untrusted HTML
Fundamentals
Introduction
Need the HTML inside a box as a string—or replace that box’s children with new markup? innerHTML does both. Reading serializes descendants; writing parses a string into a new child tree.
It is powerful and common, but MDN warns it is an injection sink: untrusted strings can become XSS. For plain text from users, prefer textContent.
Reading returns markup inside the element (not the element tag itself). For the element plus its contents, see outerHTML.
Concept
Understanding the Property
MDN: the innerHTML property of the Element interface gets or sets the HTML or XML markup contained within the element.
Get — string serialization of descendant nodes.
Set — parse HTML and replace all children.
null clears — el.innerHTML = null acts like "".
Baseline — widely available since July 2015 (MDN).
Foundation
📝 Syntax
JavaScript
innerHTML
Value
Getting returns a string of the element’s HTML contents. Setting accepts a string (or TrustedHTML where enforced) and replaces descendants with the parsed result.
Item
Detail
Type
string (get); string or TrustedHTML (set)
Access
Get and set
Replace vs insert
Use insertAdjacentHTML() to insert without full replace
Plain text
Prefer textContent for untrusted user text
⚠️
Security
Never assign untrusted HTML from users or APIs without sanitizing. Event-handler attributes (for example onerror on an image) can run script when HTML is injected.
Pattern
📋 MDN Read Example Shape
MDN reads the markup inside a container:
JavaScript
<div id="example">
<p>My name is Joe</p>
</div>
JavaScript
const myElement = document.querySelector("#example");
const contents = myElement.innerHTML;
console.log(contents);
// "\n <p>My name is Joe</p>\n" (whitespace may vary)
Related learning: outerHTML, textContent, and insertAdjacentHTML().
Cheat Sheet
⚡ Quick Reference
Goal
Code / note
Read HTML inside
el.innerHTML
Replace children
el.innerHTML = "<p>Hi</p>"
Clear
el.innerHTML = "" (or null)
Plain text only
el.textContent = userText
Insert without replace
el.insertAdjacentHTML("beforeend", html)
MDN status
Baseline Widely available
Snapshot
🔍 At a Glance
Four facts about Element.innerHTML.
Kind
get / set
Instance
Type
string
HTML markup
Set does
replace
All children
Baseline
widely
Jul 2015+
Hands-On
Examples Gallery
Examples follow MDN Element: innerHTML. Labs use trusted static strings so you can practice safely.
📚 Getting Started
Read and replace markup like MDN’s container examples.
Element.innerHTML is Baseline Widely available (MDN: across browsers since July 2015; some details vary). Logos use the shared browser-image-sprite.png sprite from this project.
✓ Baseline Widely available
Element.innerHTML
Get/set — HTML string of descendants; setting replaces all children (XSS-sensitive).
BaselineWidely available
Google ChromeSupported
Yes
Microsoft EdgeSupported
Yes
Mozilla FirefoxSupported
Yes
Apple SafariSupported
Yes
OperaSupported
Yes
Internet ExplorerSupported
Yes
innerHTMLBaseline
Bottom line: Use innerHTML to read or replace an element's HTML contents. Prefer textContent for plain text, and never assign untrusted HTML without sanitizing.
Wrap Up
Conclusion
innerHTML gets and sets the HTML inside an element. It is perfect for trusted markup updates, but treat every write as HTML parsing—use textContent for plain text and sanitize anything you do not fully control.
Only assign innerHTML with trusted or sanitized HTML
Prefer insertAdjacentHTML when appending instead of replacing
Clear with innerHTML = "" when rebuilding a container
Remember listeners on removed nodes are discarded
❌ Don’t
Pipe raw form or API strings into innerHTML
Assume <script> tags will run when injected this way
Ignore XSS risks from attributes like onerror
Use innerHTML when you only need text updates
Expect shadow roots in the serialized string
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about innerHTML
Get/set HTML inside an element—powerful, XSS-sensitive.
5
Core concepts
📄01
Get / set
on Element
Kind
📝02
HTML string
descendants
Type
🔍03
Replace
all children
Use
✅04
Baseline
widely available
Status
🎯05
XSS care
trust / sanitize
Tip
❓ Frequently Asked Questions
It gets or sets the HTML markup inside an element. Reading returns a string of the descendants; writing parses HTML and replaces all children.
No. MDN marks Element.innerHTML as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard.
Only with trusted markup. Untrusted strings can cause XSS. Prefer textContent for plain text, or sanitize / Trusted Types before injecting HTML.
textContent treats values as plain text (no HTML parsing). innerHTML parses tags into real DOM elements.
null is converted to an empty string, so el.innerHTML = null clears the element the same as el.innerHTML = "".
Use insertAdjacentHTML when you want to insert HTML next to an element without replacing its entire contents.
Did you know?
Injected <script> tags via innerHTML typically do not execute, but many other HTML patterns (like image onerror handlers) still can—so XSS risk remains real.