JavaScript Element outerHTML Property

Beginner
5 min read
Updated: Nov 2026
3 examples
Property

Definition

Element.outerHTML gets or sets the HTML of the element and its descendants (MDN). Reading serializes the element; writing replaces the element in its parent.

Read the notes below before you get or set this property. Prefer feature detection when support is limited.

Syntax

Use el.outerHTML. Start with this line.

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

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

Library declarations
JavaScript
element.outerHTML / = value

Parameters

This member has no parameters beyond those shown in the signature.

Return Value

Return ValueDescription
*The current value of el.outerHTML.

JavaScript Examples

Example 1 — Read outerHTML

Serialize an element including its own tags (MDN shape).

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

console.log(el.outerHTML);
// includes <div id="example">…</div>
Try It Yourself

How It Works

  1. Find the element. getElementById("example") returns the <div>.
  2. Read outerHTML. The browser serializes the element and its children.
  3. Tags are included. You see the opening/closing <div>, not only the inside.
  4. Compare later with innerHTML if you only need the contents.

Example 2 — Replace the Element

Assign trusted HTML to replace the node in its parent.

JavaScript
const p = document.querySelector("p");

p.outerHTML = "<div>This div replaced a paragraph.</div>";

console.log(p.nodeName); // "P"  (stale — old node)
console.log(document.querySelector("div").textContent);
// "This div replaced a paragraph."
Try It Yourself

How It Works

  1. Select a live element that has a parent in the document.
  2. Assign trusted HTML. The browser parses it and swaps the element out.
  3. Variable is stale. p still points to the old paragraph node.
  4. Re-query if you need the new node from the document.

Example 3 — outerHTML vs innerHTML

Same start node — replace inside vs replace the whole element.

JavaScript
const a = document.getElementById("a");
const b = document.getElementById("b");

a.innerHTML = "<strong>Hi</strong>";  // a stays; children change
b.outerHTML = "<strong>Hi</strong>";  // b is replaced

console.log(document.getElementById("a").tagName); // "DIV"
console.log(document.getElementById("b"));         // null
Try It Yourself

How It Works

  1. innerHTML keeps the element. Only the children are rebuilt.
  2. outerHTML replaces the element. The old id may disappear.
  3. Re-check the DOM. #a still exists; #b does not.
  4. Choose by intent. Contents only → innerHTML; whole node → outerHTML.

Browser Support

Element.outerHTML is Baseline Widely available (MDN: since July 2015). Safe across modern and older evergreen browsers.

✓ Baseline · Widely available

Element.outerHTML

Get or set HTML of the element and its descendants — setting replaces the element.

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
outerHTML Excellent

Bottom line: Safe API availability-wise. Security depends on what you assign — use trusted HTML only.

Frequently Asked Questions

It gets the HTML of an element including the element itself, or sets new HTML that replaces that element in its parent.
No. MDN marks Element.outerHTML as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard.
innerHTML is only the markup inside the element. outerHTML includes the element's own tags plus contents. Setting outerHTML replaces the element; setting innerHTML replaces its children.
The variable still references the original node, which is detached after replace. Query the document again to get the new node.
Only with trusted markup. Untrusted strings can cause XSS—same class of risk as innerHTML. Prefer textContent for plain text, or sanitize / Trusted Types.
No. Setting outerHTML on a direct child of Document throws. Replace contents of body or a container instead.

Next: part

Learn how Element.part exposes CSS export parts for shadow trees.

part 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.

16 people found this page helpful