I try to find and replace text (using jQuery). Actually I'm trying to add a span element around the text. I should also be able to remove the span again without losing the text inside.
For example, let's say I have the following situation to start with:
<span>This is a span element</span>
And I want to be able to do this dynamically using jQuery/JavaScript:
<span>This is a <span class="marked">span</span> element</span>
I should also be able to remove the span.marked and get back to the first result.
The problem however is that I want to do this on all text inside a container with multiple children and subchildren and so on.
The code I got so far will do the following and that is not what I want:
<<span class="marked">span</span>>This is a <span class="marked">span</<span class="marked">span</span> element</span>
I want to do this for ALL text on the page, not just the first it finds.
EDIT: my code so far for doing this:
var re = new RegExp(word, 'g');
$('.container').html($('.container').html().replace(re, '<span class="marked">' + word + '</span>'));
word is a string with the text to wrap spans around.