jquery - Find a string of text in an element and wrap some span tags round it -


i want find string of text in element , wrap span tags round it. eg:

from <h2>we have cows on our farm</h2> <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 wraps whole containing h2 tag.

$("h2:contains('cow')").html(function(_, html) {    return html.replace(/(cow)/g, '<span class="smallcaps">$1</span>'); }); 

http://jsfiddle.net/w5ze6/1/


Comments

Popular posts from this blog

node.js - Bad Request - node js ajax post -

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -