Just to clear the air: this
returns a native DOM element, whereas $(this)
will return its equivalent jQuery element. As .html()
is a jQuery method, it will only work on the jQuery element, not the native DOM element. So to get the HTML of an element, you will have to use either this.innerHTML
or $(this).html()
.
However; it will still not produce the result you expect. To push the full HTML of the child elements, you will need to push their outerHTML
instead.
TLDR; You will get the expected result by pushing the HTML of the native DOM element using this.outerHTML
:
$("#id").children('div').each(function(){arrayName.push(this.outerHTML)})
Or if you really want to use jQuery, you will get the same result from $(this).prop('outerHTML')
:
$("#id").children('div').each(function(){arrayName.push($(this).prop('outerHTML'))})
You can see a working sample here:
let arrayName = []
$("#id").children('div').each(function(){arrayName.push(this.outerHTML)})
console.log("First approach: ", arrayName)
let arrayName2 = []
$("#id").children('div').each(function(){arrayName2.push($(this).prop('outerHTML'))})
console.log("Second approach: ", arrayName2)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="id">
<div class="contentitem">Bob</div>
<div class="contentitem">Joe</div>
</div>