0

I have a page where I need to highlight some annotations.

Annotations Returned by Service : "Golf", "Cricket", "Ice Hockey"

Content : Peter likes to play Golf, Cricket and < b >Ice < /b > Hockey

So I used this script from this jsfiddle

$('p').highlight(['Golf','Cricket','Ice Hockey']);

This works fine till another sub tag () is encountered in between. Golf and Cricket will get Highlighted but Ice Hockey will not be highlighted. Is there a way I can make Ice Hockey also get highlighted ignoring the html tags ?

Pedram
  • 6,256
  • 10
  • 65
  • 87

2 Answers2

0

http://jsfiddle.net/Q8WS5/560/

The issue is that jQuery is calling the library three times once for the <p> until <strong> once, for the word in bold, and once again for the end.

So that library will have to go, you'll have to rewrite another one. you'll need to use .text() that returns text without html .

$('span').text().indexOf("Ice Hockey"));
ZARk
  • 53
  • 6
  • Thank you for the quick response. So I used the fiddle you shared. Content :

    Let's test highlight function

    | Highlight Terms : $('p').highlight(['highlight function']); | The text, 'Highlight Function' failed to highlight as is required. Any help will be appreciated.
    – abhisheksircar Jan 07 '16 at 10:54
  • I didn't make a full functional fiddle. Just added a couple of console.log to show you why it wouldn't work. Check out my comment on your question, with a SO link to more answers. – ZARk Jan 07 '16 at 10:59
  • jsfiddle.net/UPs3V worked well although looks like it highlights the first instance only. Looking at the code and trying to iterate. Else it works the way I wanted. – abhisheksircar Jan 07 '16 at 13:35
0

Try alternative way..

HTML

<p>Peter likes to play Golf, Cricket and <b>Ice </b> Hockey <p>

javascript

$('p').highlight(['Golf','Cricket','Ice','Hockey']);

Is this acceptable..

DEMO

Deepu Sasidharan
  • 5,193
  • 10
  • 40
  • 97
  • Actually no. We will get the terms as "Ice Hockey" together else the script will start highlighting all occurrences of Ice and Hockey as well. – abhisheksircar Jan 07 '16 at 11:07