Using jQuery to Modify First Word of a Sentance

Recently when upgrading a clients website from Joomla 1.5 to 1.7 (don’t get me started about 2.5) I found the need to add a “<span>” to each and every “<h3>” tag on the page, which would normally be pretty easy.

Due to how one of the artists designed it years back, we needed only the first word wrapped in the “<span>” tag (so we could add some color) which proved to be more challenging. Time for some jQuery Magic!

So in case anyone else reading needs to know how to modify the first word of a sentence (or header).. here it is:

var t = $(this);
t.html(t.html().replace(/^(w+)/, '$1'));

Obviously you could change the “$(‘h3’)” to be anything your heart desires.

Want to fiddle with this chunk of code a little bit ?!

Leave a Reply

Your email address will not be published. Required fields are marked *