I have problem to run function after another completes. I try callback like in this post but this dont work Execute jquery function after another function completes so maybe i do something wrong or my case is more complicated.
So I have function that run when I submit the form
$("#search-form").submit(ajaxSubmit(addSearchfuntion)); // addSearchfuntion is callback
function ajaxSubmit(callback) {
var $form = $(this);
var settings = {
data: $(this).serialize(),
url: $(this).attr("action"),
type: $(this).attr("method")
};
$.ajax(settings).done(function(result) {
var $targetElement = $($form.data("ajax-target"));
var $newContent = $(result);
$($targetElement).replaceWith($newContent);
$newContent.effect("slide");
});
callback();
return false;
};
After this when I get new form to my page i want to run another function that will handle this new form.
function addSearchfuntion(){
$('.Amount').change(updateQuantity);
}
So how to solve this case?
You need to use anonymous function to bind the submit
event handler. As of now you are executing the ajaxSubmit()
method and binding its return value i.e. false
as event handler.
$("#search-form").submit(function(){
ajaxSubmit.bind(this)(addSearchfuntion);
});
And, invoke the callback
method in the done()
callback method of $.ajax()
$.ajax(settings).done(function(result) {
// Your existing code
....
....
// call the callback
callback();
});
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments