I'm trying to wrap two words in one div no matter their positions (so basically they're treated as "start" and "end" tags).
<p>Start Tag</p>
<p>Lorem ipsum</p>
<p>Lorem ipsum</p>
<p>Lorem ipsum</p>
<p>Lorem ipsum</p>
<p>End Tag</p>
The end result should be
<div class="wrapper">
<p>Start Tag</p>
<p>Lorem ipsum</p>
<p>Lorem ipsum</p>
<p>Lorem ipsum</p>
<p>Lorem ipsum</p>
<p>End Tag</p>
</div>
I tried
$('p').each(function() {
if ($(this).text() == 'Start Tag') {
$(this).before('<div class="wrapper">');
}
if ($(this).text() == 'End Tag') {
$(this).after('</div>');
}
});
But for some reason it creates the whole div before the 'Start Tag' and does nothing after. I'm trying to split the " and the close tag "" so that it starts before the Start tag and ends after the End tag.
EDIT: Found the solution as what I needed is that sometimes there are multiple of these:
$('p:contains("Start Tag")').each(function() {
$(this).nextUntil('p:contains("End Tag:")').wrapAll('<div class="wrapper" />');
});