Languages
[Edit]
EN

JavaScript - get outer html

6 points
Created by:
Root-ssh
151380

In this article, we would like to show you how to get outer HTML source code in JavaScript.

Quick solution:

// ONLINE-RUNNER:browser;

<div id="my-element"></div>
<script>

  var myElement = document.getElementById('my-element');

  console.log(myElement.outerHTML);

</script>

 

More complex example

// ONLINE-RUNNER:browser;

<html>
<body>
  <div id="my-element">
    <h3>Head 1</h3>
    <p>Some apostrophe text...</p>
    <h3>Head 2</h3>
    <p>Some apostrophe text...</p>
  </div>
  <script>

    var myElement = document.getElementById('my-element');

    console.log(myElement.outerHTML);

  </script>
</body>
</html>

2. jQuery example

How to get outerHTML with jQuery has been described in this article.

See also

  1. JavaScript - what is difference between inner and outer html?
  2. jQuery - how to get outer html?

Alternative titles

  1. JavaScript - how to get outer html?
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join