0

I'm applying a style to a certain phrase on our website over and over again. Is there a way to search the page for instances of that phrase and apply that style automatically?

Example:

<div>
     This is what you get from <span class="comp">Company Name</span>. 
     We do all kinds of things here at <span class="comp">Company Name</span>.
</div>
nlinus
  • 641
  • 5
  • 12
  • So many duplicates: http://stackoverflow.com/questions/671574/highlight-text-in-html-with-javascript-jquery and http://stackoverflow.com/questions/119441/highlight-a-word-with-jquery and http://stackoverflow.com/questions/1230714/jquery-how-to-highlight-certain-words-with-jquery – Crescent Fresh Dec 08 '09 at 03:53

3 Answers3

2

You could take a look at these questions:

Both of the top answers point to the highlight plugin for jQuery.

Community
  • 1
  • 1
nickf
  • 537,072
  • 198
  • 649
  • 721
0

sure... http://docs.jquery.com/Selectors/contains#text

$("div:contains('Conpany Name')").css("text-decoration", "underline");
David
  • 1,887
  • 2
  • 13
  • 14
0
var body = document.getElementByTagName("body")[0].innerHTML;

body.replace(/Company Name/g, '<span class="comp">Company Name</span>');
document.getElementByTagName("body")[0].innerHTML = body;

The above should get everything in the body and replace it with your span and that doesnt need jQuery.

AutomatedTester
  • 22,188
  • 7
  • 49
  • 62
  • I think you mean ('Company Name'); or escape the middle "s, as the current code would throw an error. – Jay Dec 07 '09 at 23:52
  • 1
    `
    blah blah
    ` -> whoops. Don't process HTML with regex.Bonus: the innerHTML writeback also destroys any form data, event handlers and JS/jQuery references you have. Don't process HTML with regex.
    – bobince Dec 08 '09 at 02:56