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
  • 45,811
  • 27
  • 120
  • 136
matt
  • 40,185
  • 99
  • 257
  • 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
  • 665,005
  • 155
  • 1,345
  • 1,328