Ne...">

Jquery wrap found line inside span?

How do I look inside all h3

for a specific string like "New" and wrap it inside

<span class="red">New </span>

      

0


source to share


1 answer


Use a :contains()

selector
to find a word New

in each <h3>

, then use .html()

with a callback String.replace()

to apply <span>

:

$('h3:contains(New)').html(function() {
    return $(this).html().replace('New', '<span class="red">New</span>');
});

      

It turns

<h3>New stuff</h3>

      



in

<h3><span class="red">New</span> stuff</h3>

      

jsFiddle demo

+4


source







All Articles