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
Fundamentals
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.
Concept
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 clears — el.outerHTML = null acts like "".
Baseline — widely available since July 2015 (MDN).
Foundation
📝 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.
Item
Detail
Type
string (get); string or TrustedHTML (set)
Access
Get and set
vs innerHTML
outerHTML includes the element itself
Document root
Cannot 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.
Pattern
📋 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().
Cheat Sheet
⚡ Quick Reference
Goal
Code / note
Read full HTML
el.outerHTML
Replace the element
el.outerHTML = "<p>Hi</p>"
Remove via empty
el.outerHTML = "" (or null)
Contents only
el.innerHTML
After replace
Variable still points to the old detached node
MDN status
Baseline Widely available
Snapshot
🔍 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+
Hands-On
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());
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"
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).
BaselineWidely available
Google ChromeSupported
Yes
Microsoft EdgeSupported
Yes
Mozilla FirefoxSupported
Yes
Apple SafariSupported
Yes
OperaSupported
Yes
Internet ExplorerSupported
Yes
outerHTMLBaseline
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.
Wrap Up
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.
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
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about outerHTML
Get/set HTML of an element including itself—powerful, XSS-sensitive.
5
Core concepts
📄01
Get / set
on Element
Kind
📝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.