I'm testing a simple script where I'm storing an html string into a variable, and want to replace a part of it. My actual code is a large html string, and I cannot use replace() in this case as I have wrap() and nextSibling() functions inside my each().
<script>
var htmlvar = "<html><head></head><body><p><i>test</i></p></body></html>";
htmlvar = $(htmlvar).find('i').each(function() {
$(this).html('changed');
});
console.log('value: '+htmlvar[0].outerHTML);
</script>
After running the above the output is:
<i>changed</i>
I want to still have the full html tags after my each() function performs the logic on some of the tags. Is there any way to achieve this?
EDIT:
The same thing works with document object
<body>
<p><i>test</i>
<script>
$(document).find('i').each(function() {
$(this).wrap('<h1></h1>');
});
console.log('value: '+$(document).find('html').html());
</script>
</body>
Output is full html of the current document.