2

Possible Duplicate:
Find text string in jQuery and make it bold

Suppose if i have a html document as this

<div>
hi this is one line<br>
<p>This is second line</p>
<p>This 'word' is within quote</p>
<p>Another 'lorem ipsum' in quote</p>
</div>

How do i apply different CSS only to the word 'line' which occurs many times in my document and also different CSS to all words that appear inside quotes using jQuery. some kind of pattern matching and applying css

any script available to do so ?

Community
  • 1
  • 1
Jigar Jain
  • 1,447
  • 1
  • 15
  • 38

2 Answers2

2

You could use JQuery contains selector like this code :

http://jsfiddle.net/z4ZwF/

function replaceText(textToSearch,classToApply)
{
  $('div').html($('div').html().replace(new RegExp(textToSearch, 'g'),"<span class='"+classToApply+"'>"+textToSearch+"</span>"));
}

Example : replaceText("'lorem ipsum'",'red') or replaceText("line",'red')

EDIT

Add quote wrapping in search and replace : http://jsfiddle.net/z4ZwF/3/

function replaceText(textToSearch,classToApply,searchAndWrapQuote)
{
    searchAndWrapQuote = searchAndWrapQuote || false;
    quote = (searchAndWrapQuote) ? "'" : "";

  $('div').html($('div').html().replace(new RegExp(quote+textToSearch+quote, 'g'),"<span class='"+classToApply+"'>"+quote+textToSearch+quote+"</span>"));
}

replaceText("lorem ipsum",'red',true)
sdespont
  • 13,915
  • 9
  • 56
  • 97
0

You can't apply any style directly to 'line' word. What you need is to parse the content and wrap your 'line' to some tag - for example and after that you can apply some styles in your css to this class. You can do this in php as well as in javascript.

Dmitriy Sushko
  • 242
  • 1
  • 6