tags:

views:

54

answers:

3

Hi all,

I already use both .live() and .bind('ajaxComplete') to accomplish certain related tasks, but I have found myself wanting to be able to listen for the 'complete' event of a specific DOM element which will call jQuery's .load() at various times.

In this situation I don't want to listen for ALL complete events globally (unless someone can show me how to get the proper 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 some other code whenever that ajax call fires and completes:

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

I know that I can use what I indicated above as the "LOCAL complete function" to attach some functionality, but I would like to be able to bind to the complete event from elsewhere in my code - much like I do with other events such as 'click'.

Is there any way to do this? Or must i always make use of the 'complete' event within the context of the load() method?

A: 

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

$('.selector').load('url.php',function() {
    //Complete function
});
Dustin Laine
? according to the docs, the load() method takes an optional data object argument..load( url, [ data ], [ complete(responseText, textStatus, XMLHttpRequest) ] )EDIT: I'm sorry if the question wasn't clear, edited above thanks
DustMason
A: 

something like ?

  • ajaxComplete : Register a handler to be called when Ajax requests complete. This is an Ajax Event.
$('.selector').ajaxComplete(function() {
  //do something...
});

and of course for all the Global Ajax Event Handlers:

http://api.jquery.com/category/ajax/global-ajax-event-handlers/

aSeptik
thanks, but as i mentioned above this is not exactly what i want. although very useful, the ajaxComplete binding listens to ALL Ajax events that happen throughout the DOM. I would like to hone in on just one DOM element
DustMason
+1  A: 

Just trigger the complete event in your local complete callback:

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

$('.selector').bind('complete',function(){ ... });
PetersenDidIt
thank you, this is exactly what i needed! and such a nice simple solution
DustMason