JavaScript Element outerHTML Property

Beginner
⏱️ 12 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Baseline
Instance property

What You’ll Learn

Element.outerHTML is a read/write instance property that gets or sets the HTML of an element including the element itself. Learn how it differs from innerHTML, how setting replaces the node, and XSS caveats—with five examples and try-it labs.

01

Kind

Instance property

02

Access

Get and set

03

Type

string

04

Effect

Replace the element

05

Status

Baseline widely

06

Caution

XSS / untrusted HTML

Introduction

Want a string of a whole element (opening tag, contents, closing tag)—or replace that element with new markup? outerHTML does both. Reading serializes the node; writing parses a string and swaps the element out in its parent.

Like innerHTML, it is an injection sink: never assign untrusted HTML without sanitizing. For plain text, prefer textContent on a safe element.

JavaScript
const el = document.querySelector("#example");
console.log(el.outerHTML);
el.outerHTML = "<p class=\"note\">Replaced</p>";
💡
Beginner tip

innerHTML = markup inside the element. outerHTML = the element tag plus that markup.

Understanding the Property

MDN: the outerHTML property of the Element interface gets the serialized HTML fragment describing the element including its descendants. It can also be set to replace the element with nodes parsed from the given string.

  • Get — string of the element and its descendants.
  • Set — replace the element in its parent with the parsed result.
  • null clearsel.outerHTML = null acts like "".
  • Baseline — widely available since July 2015 (MDN).

📝 Syntax

JavaScript
outerHTML

Value

Getting returns a string serialization of the element. Setting accepts a string (or TrustedHTML where enforced) and replaces the element with the parsed nodes.

ItemDetail
Typestring (get); string or TrustedHTML (set)
AccessGet and set
vs innerHTMLouterHTML includes the element itself
Document rootCannot set on a direct child of Document (e.g. <html>)
⚠️
Security

Never assign untrusted HTML from users or APIs without sanitizing. Setting outerHTML can inject scripts and event-handler attributes just like innerHTML.

📋 MDN Read Example Shape

MDN reads the full serialization of an element:

JavaScript
<div id="example">
  <p>My name is Joe</p>
</div>
JavaScript
const element = document.getElementById("example");
console.log(element.outerHTML);
// '<div id="example">\n  <p>My name is Joe</p>\n</div>'
// (whitespace may vary)

Related learning: innerHTML, insertAdjacentHTML(), and Element.replaceWith().

⚡ Quick Reference

GoalCode / note
Read full HTMLel.outerHTML
Replace the elementel.outerHTML = "<p>Hi</p>"
Remove via emptyel.outerHTML = "" (or null)
Contents onlyel.innerHTML
After replaceVariable still points to the old detached node
MDN statusBaseline Widely available

🔍 At a Glance

Four facts about Element.outerHTML.

Kind
get / set

Instance

Type
string

Full element HTML

Set does
replace

The element itself

Baseline
widely

Jul 2015+

Examples Gallery

Examples follow MDN Element: outerHTML. Labs use trusted static strings so you can practice safely.

📚 Getting Started

Read serialization and compare with innerHTML.

Example 1 — Read outerHTML

Serialize the element including its own tags—matching MDN’s read pattern.

JavaScript
const element = document.getElementById("example");
console.log(element.outerHTML.trim());
Try It Yourself

How It Works

The getter serializes the element and its descendants. Unlike innerHTML, the opening and closing tags of the element itself are included.

Example 2 — outerHTML vs innerHTML

Side-by-side: contents only vs the whole element string.

JavaScript
const box = document.querySelector("#box");
console.log("inner:", box.innerHTML.trim());
console.log("outer:", box.outerHTML.trim());
Try It Yourself

How It Works

Use innerHTML when you only need children. Use outerHTML when you need the element wrapper too—or when you plan to replace the whole node.

📈 Replace & References

Swap an element and understand what your variable still points to.

Example 3 — Replace an Element

Assigning outerHTML removes the old element and inserts the parsed HTML in its place.

JavaScript
const old = document.getElementById("card");
old.outerHTML = '<article class="card"><strong>New card</strong></article>';
console.log(document.querySelector(".card").textContent);
Try It Yourself

How It Works

The browser parses the string into nodes and replaces the target element. Use only trusted static markup (as in this tutorial) or sanitize / Trusted Types in real apps.

Example 4 — Variable Still Points to the Old Node

After you set outerHTML, the variable still references the original detached element.

JavaScript
const el = document.getElementById("item");
el.outerHTML = "<li id=\"item\">Updated</li>";
console.log(el.parentNode); // null — old node is gone from the tree
console.log(document.getElementById("item").textContent); // "Updated"
Try It Yourself

How It Works

Setting outerHTML does not update your variable to the new node. Re-query the DOM when you need the replacement element.

Example 5 — Support Snapshot

Feature-detect and remember the XSS tip.

JavaScript
console.log({
  supported: "outerHTML" in Element.prototype,
  returns: "string (element + descendants)",
  tip: "Setting replaces the element; re-query after assign",
  status: "Baseline Widely available (MDN)"
});
Try It Yourself

How It Works

Widely supported. Treat every assignment as parsing HTML—only inject markup you trust or have sanitized.

🚀 Common Use Cases

  • Logging or debugging the full HTML of a node including its tags.
  • Replacing a card, list item, or widget with trusted static markup.
  • Removing an element by assigning an empty string (outerHTML = "").
  • Comparing outerHTML vs innerHTML when teaching DOM serialization.
  • Quick demos (prefer safer APIs in production for untrusted data).

🔧 How It Works

1

Element sits in a parent

The node has a parent that owns its place in the tree.

DOM
2

Get serializes; set parses

Read builds a full HTML string; write runs the HTML parser.

Parse
3

The element is replaced

Old node is removed; new nodes take its place in the parent.

Replace
4

Re-query if you need the new node

Your variable still holds the old detached element.

📝 Notes

  • Not Deprecated, Experimental, or Non-standard on MDN (Baseline Widely available).
  • MDN flags outerHTML as an XSS injection sink—never inject untrusted HTML raw.
  • Cannot set outerHTML on a direct child of Document (for example document.documentElement).
  • If the element has no parent, setting may leave the value unchanged.
  • Shadow roots are omitted from serialization.
  • Related: nextElementSibling, innerHTML, part, EventTarget, JavaScript hub.

Browser Support

Element.outerHTML 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.outerHTML

Get/set — HTML string of the element and descendants; setting replaces the element (XSS-sensitive).

Baseline Widely available
Google Chrome Supported
Yes
Microsoft Edge Supported
Yes
Mozilla Firefox Supported
Yes
Apple Safari Supported
Yes
Opera Supported
Yes
Internet Explorer Supported
Yes
outerHTML Baseline

Bottom line: Use outerHTML to read or replace an entire element as HTML. Prefer textContent for plain text, and never assign untrusted HTML without sanitizing.

Conclusion

outerHTML gets and sets the HTML of an element including itself. Use it to serialize or fully replace a node with trusted markup—and remember your variable still points to the old node after a set.

Continue with part, innerHTML, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Use outerHTML when you need the element tags too
  • Only assign with trusted or sanitized HTML
  • Re-query the DOM after a replace if you need the new node
  • Prefer innerHTML when only children should change
  • Prefer textContent for plain / untrusted text

❌ Don’t

  • Pipe raw form or API strings into outerHTML
  • Assume your variable updates to the replacement node
  • Try to set it on document.documentElement
  • Ignore XSS risks from attributes like onerror
  • Expect shadow roots in the serialized string

Key Takeaways

Knowledge Unlocked

Five things to remember about outerHTML

Get/set HTML of an element including itself—powerful, XSS-sensitive.

5
Core concepts
📝 02

HTML string

element + kids

Type
🔍 03

Replace

the element

Use
04

Baseline

widely available

Status
🎯 05

XSS care

trust / sanitize

Tip

❓ 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.
Did you know?

After el.outerHTML = "...", el is still the old node (usually with parentNode === null). That surprise is one of the most common beginner gotchas on MDN.

Next: part

Learn how Element.part exposes shadow DOM parts for external CSS.

part →

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.

5 people found this page helpful