New Plugin: Live Query

Sunday, August 19, 2007

Live Query, previously called Behavior, 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.

For example you could use the following code to bind a click event to all A tags, even any A tags you might add via AJAX or a script.

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

Once you add new A tags to the document, Live Query will bind the click event and there is nothing else that needs to be called or done.

When an element no longer matches a selector the events Live Query bound to it are unbound. Also, the Live Query can be expired by calling expire which will no longer bind anymore events and unbind all the events it previously bound.

Live Query can even be used with the more powerful jQuery selectors. The following Live Query will match and bind a click event to all A tags that have a rel attribute with the word “friend” in it. If one of the A tags is modified by removing the word “friend” from the rel attribute, the click event will be unbound since it is no longer matched by the Live Query.

$('a[@rel*=friend]') 
    .livequery('click', function(event) { 
        doSomething(); 
    });

Live Query also has the ability to fire a callback function when it matches a new element and another callback function for when an element is no longer matched. This provides ultimate flexibility and untold use-cases. For example the following code uses a function based Live Query to implement the jQuery hover helper method and remove it when the element is no longer matched.

$('li') 
    .livequery(function(){ 
        $(this) 
            .hover(function() { 
                $(this).addClass('hover'); 
            }, function() { 
                $(this).removeClass('hover'); 
            }); 
    }, function() { 
        $(this) 
            .unbind('mouseover') 
            .unbind('mouseout'); 
    });

See It In Action

I’ve put together two simple demos that utilize both an event based and function based Live Query on unordered lists.

Get Live Query

Live Query can be downloaded from the Live Query project page. The Live Query documentation is a work in progress but the API is fully documented.

Posted in jQuery with 4 comments

Comments

Awesome This was really annoying me, having to bind things again …

By Michael on Thursday, June 18, 2009 at 11:12 AM

Hi..really awesome plugin, but i wonder is there anyway to bind an event to a class selector, but then expire it from id selector. I’ve tried to test it but it doesnt work. e.g.

$(".myform").livequery('submit', function...)

then expire it with

$("#theform").expire('submit')

I tried this but doesnt work. Can you help me?

By Ricky on Wednesday, July  1, 2009 at 05:12 AM

@Ricky, Since LiveQuery actually binds events to the elements you should be able to unbind an event it binds on a particular element… However, it is completely unsupported and might not behave as expected in future releases.

You can do this by using unbind instead of expire.

$('#theform').unbind('submit');

By Brandon Aaron on Wednesday, July  1, 2009 at 02:15 PM

Hi Brandon…I realized that the issue appears only in IE. I bind the submit event via livequery when page loaded first time (I use class selector this time). Then when the form page called via ajax, i also called another js file to expire the previous livequery and bind my own submit handler to that form. $(“#theform”).expire(“submit”).unbind(“submit”).submit(function()…);
In firefox, this works perfect (only last submit handler is executed). But in IE, both my own submit handler and submit handler from livequery are executed.

If possible, i’ll try to setup a demo for you. Thanks a lot…:)

By Ricky on Thursday, July  2, 2009 at 04:42 AM

New Comment