Subscribe to a "complete" .load () event on a specific DOM element?

I already use both .live () and .bind ('ajaxComplete') to perform certain related tasks, but I found that I want to be able to listen for a "full" event of a specific DOM element that will call jQuery.load () at different times ...

In this situation, I don't want to listen to ALL complete events globally (unless someone shows me how to get the correct target from the event object returned by 'ajaxComplete'). I would like to be able to do something like this:

$('.selector').load('url.php',{data:data},function(){
   // LOCAL complete function
});

      

and then somewhere else, attach a handler to listen and execute other code whenever the ajax call fires and completes:

$('.selector').bind('complete',function(){ ... });

      

I know I can use what I pointed out above as "LOCAL full function" to attach some functions, but I would like to be able to bind to the whole event from somewhere else in my code - just like with others events like 'click'.

Is there a way to do this? Or should I always use the "full" event in the context of the load () method?

+2


a source to share


2 answers


Just inject the complete event into your local full callback:



$('.selector').load('url.php',{data:data},function(){
   $(this).trigger('complete');
   // Local handling of the complete event.
});

$('.selector').bind('complete',function(){ ... });

      

+2


a source


You were close to your example, see the .load () documentation in jQuery. http://api.jquery.com/load/



$('.selector').load('url.php',function() {
    //Complete function
});

      

0


a source







All Articles