The following function does the trick and keeps all the attributes. You use it for example like this: changeTag("div", "p")
function changeTag(originTag, destTag) {
while($(originTag).length) {
$(originTag).replaceWith (function () {
var attributes = $(this).prop("attributes");
var $newEl = $(`<${destTag}>`)
$.each(attributes, function() {
$newEl.attr(this.name, this.value);
});
return $newEl.html($(this).html())
})
}
}
To be sure that it works, check the following example
function changeTag(originTag, destTag) {_x000D_
while($(originTag).length) {_x000D_
$(originTag).replaceWith (function () {_x000D_
var attributes = $(this).prop("attributes");_x000D_
var $newEl = $(`<${destTag}>`)_x000D_
$.each(attributes, function() {_x000D_
$newEl.attr(this.name, this.value);_x000D_
}); _x000D_
return $newEl.html($(this).html())_x000D_
})_x000D_
}_x000D_
}_x000D_
_x000D_
changeTag("div", "p")_x000D_
_x000D_
console.log($("body").html())
_x000D_
<body>_x000D_
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>_x000D_
_x000D_
<div class="A" style="font-size:1em">_x000D_
<div class="B" style="font-size:1.1em">A</div>_x000D_
</div>_x000D_
<div class="C" style="font-size:1.2em">_x000D_
B_x000D_
</div>_x000D_
</body>
_x000D_