HTML DOM Element outerHTML
Examples
Change the first h2 element and it's content:
document.getElementsByTagName("h2")[0] = "<h3>Changed!</h3>";
Try it Yourself »
Replace an element with a header:
element.outerHTML = "<h2>This is a h2 element</h2>";
Try it Yourself »
Alert the outer HTML of a <h1> element:
let html = document.getElementsByTagName("h1")[0].outerHTML;
alert(html);
Try it Yourself »
Alert the outer HTML of a <ul> element:
let html = document.getElementsByTagName("ul")[0].outerHTML;
alert(html);
Try it Yourself »
Description
The outerHTML
property sets or returns the HTML element,
including attributes, start tag, and end tag.
Syntax
Return the outerHTML property:
element.outerHTML
Set the outerHTML property:
element.outerHTML = text
Property Value
Value | Description |
text | The new HTML content. |
Return Value
Type | Description |
String | The HTML content of the element, including attributes, start tag and end tag. |
Browser Support
element.outerHTML
is supported in all browsers:
Chrome | Edge | Firefox | Safari | Opera | IE |
Yes | Yes | Yes | Yes | Yes | Yes |