I need to create a way to encapsulate all non-html words in a html page with html. An example:
<p id="paragraph"><a href="http://www.google.com">Google it!</a>But I <b>must</b> explain to you</p>
should be changed to
<p id="paragraph"><a href="http://www.google.com"><span id="word1">Google</span> <span id="word2">it!</span></a><span id="word3">But</span> <span id="word4">I</span> <b><span id="word5">must</span></b> <span id="word6">explain</span> <span id="word7">to</span> <span id="word8">you</span></p>
I have tried to extract all words:
group_text = $("#paragraph").text().trim().split(" ");
and then encapsulate each word with the selected html, but that removes all other existing html the document might have
for (var it = 0; it < group_text.length; it++) {
group_text[it] = $('<span/>', {
id: 'word' + (it+1),
html: group_text[it]
}).append(" ");
}
Any solution that might actually work?