views:

30

answers:

2

Is there a way to use jQuery to search a <p> and do something to each occurrence of a string.
For example make every string "magic" in the page bold?
Is there a way to do it for a character so that every 'a' could be made bold? It appears contains just gives the element that contains the text and not the string itself.

A: 

http://www.jquery.info/spip.php?article50

this plugin will do the bolding example. If you need it to do something else, you can modify the plugin as needed.

Mike Sherov
+1  A: 

I think i'd use a combination of JQuery and JS regexps: JQ to find the elements to process and get the contents out of each, JavaScript's replace() method to do the string manipulation, and JQ to put the modified contents back in the DOM elements.

$('p').each(function(){
    $(this).html($(this).html().replace(/(bold)/,'<b>$1</b>'));
});
fsb