don
don

Reputation: 4532

jQuery .on() at ajax success

I would like to bind .on() to an ajax "event", meaning that I'd like it to be triggered whenever an ajax response is successfully retrieved. I don't get how I could bind .on() to such an event. Do I need to use load like so?

$(document).on('load','#elementInsertedByAjax',function(){
    // will do something
});

PS I do need to use .on() since the whole page is inserted by an ajax call.

Upvotes: 7

Views: 21506

Answers (3)

coolguy
coolguy

Reputation: 7954

$,ajax({
  url:'yoururl',
  type : 'GET', /*or POST*/
  data: {'d1',data1}, /*if any*/
  success:function(returndatafromserver){
         //here you can check the success of ajax
  },error:function(errormsg){

   //error if any

  }

});

Upvotes: 0

Olaf Dietsche
Olaf Dietsche

Reputation: 74028

You can use .ajaxSuccess()

$(document).ajaxSuccess(function() {
// will do something
});

or bind to

$(document).on('ajaxSuccess','#elementInsertedByAjax',function(){
    // will do something
});

Upvotes: 15

Arun P Johny
Arun P Johny

Reputation: 388326

You can use the ajax global event jQuery.ajaxSuccess

$(document).ajaxSuccess(function() {
    $( ".log" ).text( "Triggered ajaxSuccess handler." );
});

Upvotes: 1

Related Questions