I'm using Jquery 3.4.1. I'm trying to replace the img tag in my html with dynamically created div element. But it is always getting rendered as plain text instead of html.
HTML:
<p class="post-content">
<img src="..." />
</p>
Script:
$.each($('.post-content img'), function () {
const imgSrc = this.src;
this.closest('p').replaceWith(`<div class="post-image"><div class="post-image-cover" style="background-image: url(${imgSrc})"></div></div>`)
});
Current Output:
Expected Output:
Above text html needs to be rendered as HTML element.
Please assist on where I'm wrong?

.html()and not.replaceWith(), this will maybe solve the problem you are facing. Looks like if you do$(this).closest('p').html()it worksptag with my html. I don't want to place the element insideptag.$(this).closest('p')[0].outerHTML = '<div class