Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use jQuery .live() with ajax

Currently I am using John Resig's LiveQuery plugin/function to allow users to sort through a long unordered-list of list-items. The code is as follows: $('input#q').liveUpdate('ul#teams').focus(); The issue arises when I use ajaxified tabs to sort the lists. Essentially I use ajax to pull in different lists and the liveUpdate() function doesn't have access to the new li's.

I assume I would need to bind this using the .live() function. But I am unclear how to bind this to an ajax event, I've only used the "click" event. How would I bind the new liveUpdate() to the newly loaded list-items?

EDIT: The ajax tabs is run through the wordpress ajax api so the code is fairly complex, but simplified it is something like this:

$('div.item-list-tabs').click( function(event) {
    var target = $(event.target).parent();

    var data = {action, scope, pagination}; // Passes action to WP that loads my tab data
    $.post( ajaxurl, data, function(response) {
        $(target).fadeOut( 100, function() {
            $(this).html(response);
            $(this).fadeIn(100);
        });
     });

     return false;
});

This is simplified for the sake of this conversation, but basically once the $.post loads the response in place .liveUpdate() doesn't have access to it. I believe the .live() function is the answer to this problem, I'm just unclear on how to implement it with the $.post()

like image 802
kylemac Avatar asked Mar 04 '10 03:03

kylemac


People also ask

Can you use jQuery with Ajax?

What About jQuery and AJAX? jQuery provides several methods for AJAX functionality. With the jQuery AJAX methods, you can request text, HTML, XML, or JSON from a remote server using both HTTP Get and HTTP Post - And you can load the external data directly into the selected HTML elements of your web page!

What is the use of live in jQuery?

jQuery | live() Method This method is used to attach one or more event handlers for the selected elements. It also specifies the function that runs when the event occurs. The event handlers used will work for both current and future elements matching the selector.

How does .post work in jQuery?

jQuery $.post() Method The $.post() method requests data from the server using an HTTP POST request. Syntax: $.post(URL,data,callback); The required URL parameter specifies the URL you wish to request.

How do I live in JavaScript?

Use the on() method instead. The live() method attaches one or more event handlers for selected elements, and specifies a function to run when the events occur. Event handlers attached using the live() method will work for both current and FUTURE elements matching the selector (like a new element created by a script).


2 Answers

As mentionned in the documentation of JQuery, .live() is considered deprecated. You should use the method .on() instead.

$("#yourSelector").on("click", function() {
    // statement
});

To also manage futur object of the selector type, you can use .on() like this

$(document).on("click", "#yourSelector", function() {
    // statement
});
like image 145
maniak Avatar answered Oct 14 '22 18:10

maniak


Try using jQuery's Ajax Events

$('input#q').ajaxComplete(function(){
    $(this).liveUpdate('ul#teams').focus();

    $(this).unbind('ajaxStop');
});
like image 26
relu Avatar answered Oct 14 '22 18:10

relu