outerHTML in javascript

outerHTML property in javascript

outerHTML property is used to get the specified HTML element including all its descendant content.

This property can also be used to replace the contents in an element using the given one.

In order to return only the descendant content, innerHTML can be used.

Syntax

var elem = element.outerHTML;

Example - Getting the outerHTML of an element

Try Yourself

Example Explanation

  • In the above example, when the button is clicked fun_name() function is called.
  • In the Javascript function, first we are getting the element (document.getElementById("idDiv")) and storing it in attr variable.
  • Using the outerHTML property, we are getting the whole element and displaying it (alert(elem.outerHTML)).