I have this html code:
<p><span>h</span><span>o</span><span>l</span><span>a</span></p>
And I use jQuery on it to replace all the gaps:
$('p span').each(function(){
$(this).replaceWith($(this).text());
});
When I look through my DOM, I see a script created 4 text nodes for each letter. How can I prevent this? I want only 1 text node!
Note: this example is very simplified. I actually do this:
<p>This is an <b>example</b>: <span>h</span><span>o</span><span>l</span><span>a</span>!</p>
It should look like this:
<p>{text}This is an {/text}<b>{text}example{/text}</b>{text}: hola!{/text}</p>
{text} is the DOM text of node :-)
source
share