0

trying to run jquery plugin "livequery" to highlight some words in a dynamically generated search results doesn't work ! However adding an alert() function before excuting the code make the highlighting appear! so what is the problem?

$(document).ready(function(){     
     $('#searchResults').livequery(function(el){
     // alert('test');
     $( '#searchResults' ).highlight( highlightArray );
 });
});  
Newbie
  • 69
  • 2
  • 8

2 Answers2

1

Can you try adding some delay by setTimeout()

$(document).ready(function(){     
     $('#searchResults').livequery(function(el){
         // alert('test');
         setTimeout(function(){
             $( '#searchResults' ).highlight( highlightArray );
         },400);
     });
});  
vijayP
  • 11,432
  • 5
  • 25
  • 40
  • nice idea! however it has some disadvantages like if the page content takes a longer time than specified, it will not run...it is really nice idea but I hope I can find any better one. – Newbie Sep 03 '15 at 12:13
1

Why do you still use livequery? It is not necessary by now. It was before jQuery delegated events. See this SO answer for more informations. Use .on() instead of livequery().

So you can just do

$(document).on('change','#searchResults',function(el){
    $('#searchResults').highlight(highlightArray);
});
Community
  • 1
  • 1
Lelio Faieta
  • 6,457
  • 7
  • 40
  • 74
  • I tried something like $(document).on('change','#searchResults',function(){ alert("test"); }); – Newbie Sep 03 '15 at 12:20
  • still not working, the alert does not show up, idon't know why (Im 100% sure that #searchResults Id is the targeted div) – Newbie Sep 03 '15 at 12:21
  • Have a closer look at how .on() works. You don't have to look for the target div but for the element that triggers the event. Event can be any and triggering element too. Inside you will run the animation on any element in the Dom – Lelio Faieta Sep 03 '15 at 12:33
  • I tried "body" instead of "#searchResults" still no alert aplears although new data (seatch results) always appear on the page – Newbie Sep 03 '15 at 12:43
  • Body is not the element to refer to. Please set up a fiddle if you want further debug and have a look at the documentation for .on – Lelio Faieta Sep 03 '15 at 12:48