Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 531
don Avatar asked Jan 30 '13 10:01

don


People also ask

What is success in jQuery AJAX?

What is AJAX success? AJAX success is a global event. Global events are triggered on the document to call any handlers who may be listening. The ajaxSuccess event is only called if the request is successful. It is essentially a type function that's called when a request proceeds.

How do you call a function after a success on AJAX?

Call function after ajax complete You can simply copy/paste code in the body tag of the html page and reload page to get the content form https://api.joind.in/v2.1/talks/10889 URL after success and call callFunctionAfterAjaxCallComplete() function after ajax request is completed.

How do I return a successful AJAX?

You can store your promise, you can pass it around, you can use it as an argument in function calls and you can return it from functions, but when you finally want to use your data that is returned by the AJAX call, you have to do it like this: promise. success(function (data) { alert(data); });

What triggers AJAX success?

What triggers Ajax success? Whenever an Ajax request completes successfully, jQuery triggers the ajaxSuccess event. Any and all handlers that have been registered with the . ajaxSuccess() method are executed at this time.


2 Answers

You can use .ajaxSuccess()

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

or bind to

$(document).on('ajaxSuccess','#elementInsertedByAjax',function(){
    // will do something
});
like image 198
Olaf Dietsche Avatar answered Nov 14 '22 14:11

Olaf Dietsche


You can use the ajax global event jQuery.ajaxSuccess

$(document).ajaxSuccess(function() {
    $( ".log" ).text( "Triggered ajaxSuccess handler." );
});
like image 1
Arun P Johny Avatar answered Nov 14 '22 13:11

Arun P Johny