HTML DOM Element outerHTML attribute

Definition and usage

outerHTML Property to set or return HTML elements, including attributes, start tags, and end tags.

Instance

Example 1

Change the first h2 element and its content:

document.getElementsByTagName("h2")[0] = "<h3>Changed!</h3>";

Try it yourself

Example 2

Replace the element with a title:

element.outerHTML = "<h2>This is a h2 element</h2>";

Try it yourself

Example 3

Output the external HTML of the <h1> element:

let html = document.getElementsByTagName("h1")[0].outerHTML;
alert(html);

Try it yourself

Example 4

Output the external HTML of the <ul> element:

let html = document.getElementsByTagName("ul")[0].outerHTML;
alert(html);

Try it yourself

Syntax

Return the outerHTML property:

element.outerHTML

Set the outerHTML property:

element.outerHTML = text

Attribute value

Value Description
text New HTML content.

Return value

Type Description
String The HTML content of the element, including attributes, start tags, and end tags.

Browser Support

All Browsers Support element.outerHTML:

Chrome IE Edge Firefox Safari Opera
Chrome IE Edge Firefox Safari Opera
Support Support Support Support Support Support