0

How do I look inside of all h3's for a specific string like "New" and wrap that inside of a

<span class="red">New </span>
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
matt
  • 42,713
  • 103
  • 264
  • 397

1 Answers1

4

Use the :contains() selector to find the word New in every <h3>, then use .html() with a callback to String.replace() to apply the <span>:

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

This turns

<h3>New stuff</h3>

into

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

jsFiddle demo

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356