JavaScript Element innerHTML Property

Beginner
5 min read
Updated: Nov 2026
3 examples
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.

JavaScript
element.innerHTML / = value
CallWhat it does
element.innerHTML / = valuePrimary signature.

Built into the browser. Read or assign el.innerHTML — no import required.

Library declarations
JavaScript
element.innerHTML / = value

Parameters

ParameterDescription
ClearOptional. el.innerHTML = "" (or null)

Return Value

Return ValueDescription
*The current value of el.innerHTML.

JavaScript Examples

Example 1 — Read innerHTML

Serialize the HTML inside a container (MDN shape).

JavaScript
const el = document.getElementById("example");

console.log(el.innerHTML);
// includes the nested <p> markup
Try It Yourself

How It Works

  1. Find the container. getElementById("example") returns the <div>.
  2. Read innerHTML. The browser serializes the child nodes to a string.
  3. Tags stay as markup. You see <p>…</p>, not only the visible text.
  4. Whitespace may appear from pretty-printed HTML (newlines/spaces).

Example 2 — Replace Contents

Assign trusted HTML to wipe and rebuild the children.

JavaScript
const box = document.getElementById("box");

box.innerHTML = "<p>Hello</p><p>World</p>";
console.log(box.children.length); // 2
console.log(box.innerHTML);
Try It Yourself

How It Works

  1. Assign a string. The browser parses it as HTML.
  2. Old children are removed. Everything inside box is replaced.
  3. New elements appear. Two <p> nodes → children.length is 2.
  4. Trust the source. Never assign raw user input this way.

Example 3 — innerHTML vs textContent

Same string, two meanings — tags vs literal text.

JavaScript
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>"
Try It Yourself

How It Works

  1. innerHTML parses tags. You get a real <strong> child.
  2. textContent does not. Angle brackets become visible text.
  3. Child counts differ. One element child vs none.
  4. Rule of thumb. User text → textContent; trusted HTML structure → innerHTML.

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.

99% Widely available
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

It gets or sets the HTML markup inside an element. Reading returns a serialization 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.

Next: lastElementChild

Learn how to read the last child Element of a parent.

lastElementChild tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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