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()
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!
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.
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.
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).
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
});
Try using jQuery's Ajax Events
$('input#q').ajaxComplete(function(){
$(this).liveUpdate('ul#teams').focus();
$(this).unbind('ajaxStop');
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With