Live query

Live Query

Nov 26th in Plugins

If you've ever dynamically created an element using Javascript, you'll no doubt have experienced problems with binding events to these elements. Luckily, a small jQuery plugin created by Brandon Aaron (jQuery team member), called Live Query, makes this a cinch.

"Live Query utilizes the power of jQuery selectors by binding events or firing callbacks for matched elements auto-magically, even after the page has been loaded and the DOM updated."

Implementation

$('a') 
    .livequery('click', function(event) { 
        alert('clicked'); 
        return false; 
    });

This will fire the click event on all anchor tags - even the ones that have been dynamically generated with AJAX. For more information, refer to the documentation.


Related Freebies

Download some more awesome freebies!

Enjoy this Post?

Your vote will help us grow this site and provide even more awesomeness

User Comments

( ADD YOURS )
  1. PG

    Shaun November 26th

    This will definitely solve some problems of mine.

    ( Reply )
  2. PG

    Jhay November 26th

    Great!!!!

    ( Reply )
  3. PG

    WebDevVote November 26th

    Simple but useful!!!

    ( Reply )
  4. PG

    Daniel November 26th

    You should probably use event delegation instead where you can as it’s lighter and faster as a technique.

    ( Reply )
  5. PG

    celebrus November 26th

    This is cool. :)

    Will probably help me a lot.

    ( Reply )
  6. PG

    Jewe November 26th

    What is the difference or the advantage to the bind function?

    ( Reply )
  7. PG

    Sacha November 26th

    This can really slow down your apps ! be careful !

    ( Reply )
  8. PG

    Darren November 26th

    Perfect! had that problem many a times!

    ( Reply )
  9. PG

    James November 26th

    This is a cool plugin but those using it should be aware that it only works if you add your elements using jQuery, – all the plugin actually does is extend jQuery’s append/html/prepend/insertBefore etc. etc. functions.

    So, if you were to add an element using JavaScript’s native “elem.appendChild(document.createElement(’a'))” this plugin would be ineffective.

    ( Reply )
  10. PG

    Joe Holdcroft November 27th

    I’m with Daniel, event delegation is the way to go for speed and general best practice.

    ( Reply )
  11. PG

    kareem November 27th

    this is wonderful tutorial i will put acopy of this lesson on
    my site here
    http://www.as7ap4you.com

    ( Reply )
  12. PG

    Cerebral November 27th

    Event delegation, all the way

    ( Reply )
  13. PG

    Jamie Hill November 27th

    +1 for event delegation. We use it heavily on http://laurelandhardyarchive.com

    ( Reply )
  14. PG

    Sudhir November 27th

    hmmmm… wouldn’t it be better to use event delegation on the parent element being updated?

    ( Reply )
  15. PG

    Chris Gunther December 1st

    I’ve used LiveQuery on my site to handle the issue of dynamically added content but now I’ll have to look into using event delegation instead. Thanks for pointing out that alternative.

    ( Reply )
  16. PG

    gogi December 8th

    hi, i don’t understand it much. but does this mean i have to install a plugin for firefox/ie before i can use this one?

    ( Reply )
  17. PG

    Christopher December 8th

    I’ve used LiveQuery on a recent side project and heard about the problems, so I attempted to try out Delegation, but I couldn’t figure out the syntax. Anyone know of a good explanation/tutorial on how to use it? I’m not a total newbie, but I couldn’t figure it out. If it’s the lighter, faster solution, I feel like I should know more. Maybe Net Tuts could look into this???

    ( Reply )
  18. PG

    Gilles Ruppert December 10th

    @Christopher: there is no specific syntax to event delegation. Christian Heilmann has a good post about here:
    http://icant.co.uk/sandbox/eventdelegation/
    & Dan Webb wrote a jQuery plugin
    http://www.danwebb.net/2008/2/8/event-delegation-made-easy-in-jquery

    What I tend to do is bind the event on the parent & then check for classes of the target. In jQuery code it looks similar to this:

    $('ul').click(function(e){
    var $t = $(e.target); // converts the target into a jQuery object

    // this kind of code is needed if you want to do something different depending on the item clicked
    if ($t.hasClass('.next')) {
    getNext($t);
    } else if ($t.hasClass('.prev')) {
    getPrev($t);
    }

    // or you can do this if you just want to do the same thing
    $t.css('color', 'red'); // makes the clicked element red

    return false; // stop the event propagation & default action old school way
    });

    This is just a quick example & should not be used in production code (it might have some errors in it, I just quickly typed it into the box);

    Hope this is helpful

    ( Reply )
  19. PG

    Jmmm December 14th

    Thx a lot ! It was really missing..

    ( Reply )
  20. PG

    Nate January 29th

    Broken demo link. It is possible to have a report broken links button on this site?

    ( Reply )
  21. PG

    Groningen February 9th

    Realy nice script, helps realy if you don’t want to run a dispatcher again

    ( Reply )
  22. PG

    Electric Graffiti June 11th

    Awesome… Just what i needed

    ( Reply )
  23. PG

    oyanamoy September 4th

    why do we have to add the “return false;” statement at the end of the function after using alert() ?

    ( Reply )
  1. Arrow
    Gravatar

    Your Name
    September 4th