I want to find a string of text in an element and wrap some span tags round it. Eg:
From <h2>We have cows on our farm</h2>
to <h2>We have <span class='smallcaps'>cows</span> on our farm</h2>
I've tried:
$("h2:contains('cow')").each(function() {
$(this).text().wrap("<span class='smallcaps'></span>");
});
But that only wraps the whole containing h2 tag.
Another approach, split by keyword and join with the updated html.
Note: I haven't tested this, but I'm assuming this would perform worse than doing a replace, but figure I would include anyways for informative purposes
Here is a variation on @undefined's answer, which loops through an array of items:
http://jsfiddle.net/w5ze6/1/
In my case i have some tag inside the target div and some text i need to wrap that text into a link.
This how i did following "What have you tried".