-4

i have a text like this:

<p>Hi this is a text and i wan't to add a class to some words which are here</p>

And I wan't to add class ".red" to this words: "some","here","add", for example. How can I make this with jQuery? The easier the code the best it is.

mrpinkman
  • 201
  • 3
  • 14
  • 4
    This should help: http://stackoverflow.com/questions/119441/highlight-a-word-with-jquery – Joe May 03 '14 at 20:25
  • that's uses js, not jquery, and I can't understand that code, i just want a simple solution like this, i'm not sure but i think it can be done with "find()" – mrpinkman May 03 '14 at 20:28
  • 1
    You can't use `.find()` to access anything other than elements, and individual words aren't elements. jQuery *is* JavaScript. – JJJ May 03 '14 at 20:29
  • 1
    See here http://stackoverflow.com/questions/9167855/highlight-text-inside-html-with-jquery – microspino May 03 '14 at 20:29

1 Answers1

1

You can use this code :

$("p").each().function(){
    $this=$(this);
    if($this.html().indexOf("some") > -1)
        $this.addClass("red");
}
Saman Gholami
  • 3,416
  • 7
  • 30
  • 71