Home JavaScript Element Properties innerHTML JavaScript Element innerHTML Property Definition Element.innerHTML gets or sets the HTML markup inside an element (MDN). Reading serializes descendants; writing parses HTML and replaces all children.
Read the notes below before you get or set this property. Prefer feature detection when support is limited.
Syntax Use el.innerHTML. Start with this line.
element.innerHTML / = value Call What it does element.innerHTML / = valuePrimary signature.
Built into the browser. Read or assign el.innerHTML — no import required.
Library declarations element.innerHTML / = value Parameters Parameter Description Clear Optional. el.innerHTML = "" (or null)
Return Value Return Value Description * The current value of el.innerHTML.
JavaScript Examples 1. Read innerHTML 2. Replace contents 3. vs textContent Example 1 — Read innerHTML Serialize the HTML inside a container (MDN shape).
const el = document.getElementById("example");
console.log(el.innerHTML);
// includes the nested <p> markup How It Works Find the container. getElementById("example") returns the <div>.Read innerHTML. The browser serializes the child nodes to a string.Tags stay as markup. You see <p>…</p>, not only the visible text.Whitespace may appear from pretty-printed HTML (newlines/spaces).Example 2 — Replace Contents Assign trusted HTML to wipe and rebuild the children.
const box = document.getElementById("box");
box.innerHTML = "<p>Hello</p><p>World</p>";
console.log(box.children.length); // 2
console.log(box.innerHTML); 2
<p>Hello</p><p>World</p> How It Works Assign a string. The browser parses it as HTML.Old children are removed. Everything inside box is replaced.New elements appear. Two <p> nodes → children.length is 2.Trust the source. Never assign raw user input this way.Example 3 — innerHTML vs textContent Same string, two meanings — tags vs literal text.
const a = document.getElementById("a");
const b = document.getElementById("b");
const s = "<strong>Hi</strong>";
a.innerHTML = s; // creates a <strong> element
b.textContent = s; // shows the tags as text
console.log(a.children.length); // 1
console.log(b.children.length); // 0
console.log(b.textContent); // "<strong>Hi</strong>" How It Works innerHTML parses tags. You get a real <strong> child.textContent does not. Angle brackets become visible text.Child counts differ. One element child vs none.Rule of thumb. User text → textContent; trusted HTML structure → innerHTML.Compatibility
Browser Support Element.innerHTML is Baseline Widely available (MDN: since July 2015). Some serialization edge cases differ slightly by browser.
✓ Baseline · Widely available
Element.innerHTML Get or set HTML inside an element — replace children; treat untrusted input carefully.
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support innerHTML Excellent
Bottom line: Safe API availability-wise. Security depends on what you assign — use trusted HTML only.
Frequently Asked Questions What does element.innerHTML do? It gets or sets the HTML markup inside an element. Reading returns a serialization of the descendants; writing parses HTML and replaces all children.
Is innerHTML deprecated or experimental? No. MDN marks Element.innerHTML as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard.
Is assigning innerHTML safe? Only with trusted markup. Untrusted strings can cause XSS. Prefer textContent for plain text, or sanitize / Trusted Types before injecting HTML.
How is it different from textContent? textContent treats values as plain text (no HTML parsing). innerHTML parses tags into real DOM elements.
What does setting null do? null is converted to an empty string, so el.innerHTML = null clears the element the same as el.innerHTML = "".
When should I use insertAdjacentHTML? Use insertAdjacentHTML when you want to insert HTML next to an element without replacing its entire contents.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
18 people found this page helpful
Helpful Share Copy link Suggestion