Vika Vika - 3 months ago 9
jQuery Question

Jquery unwrap in each

I have a problem with my unwrap, here is my HTML code :

<div id="MyDiv">
<span class="highlight" id="highlight_36">bolo</span>
caret
<span class="highlight" id="highlight_37">lapso</span>
bla bla bla bla
</div>


If i execute :

$(".highlight").each(function() {
// complicated operation
...

$(this).contents().unwrap();
});


After my each, (in my browser inspector) HTML code becomes :

<div id="MyDiv">
bolo
caret
lapso
bla bla bla bla
</div>


How to obtain this HTML code ?

<div id="MyDiv">bolo caret lapso bla bla bla bla</div>

Answer

I don't see why you need this, but if you really need to, you can use this after your operation:

var el = document.getElementById('MyDiv');
el.innerHTML = el.innerHTML.replace(/(\s|\r|\n)+/g, ' ').trim();

That will replace every doublespaces, linebreaks and tabulations from you string and reasign it the the element.

Comments