Favorites

DOM Element.outerHTML Property

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee
Element.outerHTML
DOM Element PropertyThis property is supported by all modern browsers.
Updated 28th November 2026

The outerHTML property gets the HTML of an element including its own tag, or replaces the whole element when set.

Description

The outerHTML property returns the HTML of the element including its own opening and closing tags. Setting it replaces the element in the page with the parsed markup; your variable still refers to the old, now-detached element. The same security caution as innerHTML applies.

Setting outerHTML throws an error for the <html> element and elements without a parent, since there's nothing to replace them in. Any event listeners on the old element and its children are lost, because new elements are created from the markup.

Reading it is handy for debugging, logging and copying an element's markup. To replace an element safely with new nodes, without parsing HTML strings, use replaceWith().

Syntax

element.outerHTML
element.outerHTML = htmlString

Value

A string of the element's HTML, including the element itself. Setting it replaces the element.

Example

<button class="primary" type="button">Save</button>
const btn = document.querySelector('button');
console.log(btn.outerHTML);
btn.outerHTML = '<a href="#">Now a link</a>';
console.log(document.body.innerHTML.trim());

Browser Support

The following information will show you the current browser support for the Element.outerHTML property. Hover over a browser icon to see the version that first introduced support for this property.

This property is supported by all modern browsers.

Desktop

Chrome
Edge
Firefox
Opera
Safari

Tablets & Mobile

Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-
Supported Unknown Not supported
☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee