I have tried to find answers and only stumbled over replaceWith Automatically Closes the Tag and Replace HR Element with Open Div - both finding first and last element and wrapping a div
around.
In my case we have an HTML with multiple text in p
elements, such as:
<p>
[spoiler]
</p>
<p>
Text to hide.
</p>
<p>
[/spoiler]<br />
</p>
<p>
Text in-between.
</p>
<p>
[spoiler]
</p>
<p>
Second Text to hide.
</p>
<p>
[/spoiler]
</p>
<p>
More Text …
</p>
Now I need to find the opening [spoiler]
tags and replace their wrapping p
tags with an opening <div class="spoiler">
. Next I need to find the closing [/spoiler]
tags and replace them with a closing </div>
.
Jquery's replaceWith()
always closes the inserted element.
So my simple approach does not work:
$(this).find('p:contains([spoiler])').replaceWith('<div class="qa-spoiler">');
$(this).find('p:contains([/spoiler])').replaceWith('</div>');
I have no control about the HTML markup.
How to do it?
PS: Changed the headline from "replaceWith without closing Tag and wrapping a Div around multiple p tags" to "How to replace an HTML tag with only an opening DIV and close it later on".