I am trying to find and replace text (using jQuery). I'm actually trying to add a span element around the text. I would also have to delete the breach 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 this to be able to be dynamically used using jQuery / JavaScript:
<span>This is a <span class="marked">span</span> element</span>
I will also be able to remove span.marked and return to the first result.
However, the problem is that I want to do this in all the text inside the container with several children and children, etc.
The code I received so far will do the following, and this 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 of the text on the page, and not just for the first.
EDIT: my code so far for this:
var re = new RegExp(word, 'g'); $('.container').html($('.container').html().replace(re, '<span class="marked">' + word + '</span>'));
A word is a line with text to wrap around.
source share