This post will discuss how to get outerHTML of an element in JavaScript and jQuery.

The outerHTML is often used to replace the element and its contents completely. It differs from the innerHTML as innerHTML only represent the HTML of contents of an element, while outerHTML includes the HTML of element itself with its descendants.

1. Using JavaScript

In JavaScript, you can easily get the value of an element’s outerHTML with the outerHTML attribute.

JS


HTML



Edit in JSFiddle

2. Using jQuery

With jQuery, you can access the outerHTML attribute of HTML using the $(selector).prop() or $(selector).attr() method.

JS


HTML



Edit in JSFiddle

 
Alternatively, you can access the underlying HTML of the object returned by the $(selector) and get its .outerHTML property.

JS


HTML



Edit in JSFiddle

That’s all about getting outerHTML of an element in JavaScript and jQuery.