0

I want to get the entire HTML from a element in HTML, I'm doing like this:

const divider = $('#divider').html()
$('#htmlElement').innerHTML = divider
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div id="divider">
  <span>Test</span>
</div>

<span id="htmlElement"></span>

But it returns the children element from the <div></div>, not the entire HTML selected by the id.

How can I get the entire HTML text from the selected element using jQuery? Including his class and all inlined styles?

Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
  • 2
    jquery doesn't have this, but you can use native js `$("#divider")[0].outerHTML` https://developer.mozilla.org/en-US/docs/Web/API/Element/outerHTML – freedomn-m Mar 10 '21 at 17:52
  • And many others (the term you're looking for to search for is "outer html" or "outerHTML") - https://stackoverflow.com/search?q=%5Bjquery%5D+outerhtml – freedomn-m Mar 10 '21 at 17:55
  • 1
    Here is a quick and dirty jQuery plugin: `($ => $.fn.outerHTML = function() { return this.get(0).outerHTML })(jQuery)` You can call it via: `$('#divider').outerHTML()` – Mr. Polywhirl Mar 10 '21 at 17:59

0 Answers0