how to run function after another one completes

cyprian

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?

Satpal

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Run one rake task after another completes

From Dev

Running one function after another completes

From Dev

How to make a javascript function to run after another one get finished?

From Dev

Run one function after another: callback issue

From Dev

Run function after other function completes

From Dev

Execute jQuery function after another function completes

From Dev

Execute jQuery function after another function completes

From Dev

Run function after CSS transition completes

From Dev

How can I run one gulp function after another in a javascript function?

From Dev

How to run second Javascript function only after first function fully completes?

From Dev

Hide one button and show another one after ajax completes

From Dev

Hide one button and show another one after ajax completes

From Dev

How can I have one systemd service trigger another after it completes?

From Dev

How to display output of one function after another

From Dev

Run another task after executing one function successfully Using Celery

From Dev

Run a thread in android after one thread completes its execution

From Dev

jquery start a function only after one function completes

From Dev

jquery start a function only after one function completes

From Java

HTML load one script after another script completes execution

From Dev

Run code after function completes and get returned value

From Dev

Run code after function completes and get returned value

From Dev

how to run multiple jobs one after another in jenkins

From Dev

How to run multiple python file in a folder one after another

From Dev

How do i run a for loop in bash one after another

From Dev

How to load 2 Javascript files async and run one after another?

From Dev

How to run one statement after another in a procedural manner in Javascript?

From Dev

How to run function after reload to another page from special link?

From Dev

How to run php function after redirecting user to another page?

From Dev

How to redirect to another page after AJAX request completes

Related Related

  1. 1

    Run one rake task after another completes

  2. 2

    Running one function after another completes

  3. 3

    How to make a javascript function to run after another one get finished?

  4. 4

    Run one function after another: callback issue

  5. 5

    Run function after other function completes

  6. 6

    Execute jQuery function after another function completes

  7. 7

    Execute jQuery function after another function completes

  8. 8

    Run function after CSS transition completes

  9. 9

    How can I run one gulp function after another in a javascript function?

  10. 10

    How to run second Javascript function only after first function fully completes?

  11. 11

    Hide one button and show another one after ajax completes

  12. 12

    Hide one button and show another one after ajax completes

  13. 13

    How can I have one systemd service trigger another after it completes?

  14. 14

    How to display output of one function after another

  15. 15

    Run another task after executing one function successfully Using Celery

  16. 16

    Run a thread in android after one thread completes its execution

  17. 17

    jquery start a function only after one function completes

  18. 18

    jquery start a function only after one function completes

  19. 19

    HTML load one script after another script completes execution

  20. 20

    Run code after function completes and get returned value

  21. 21

    Run code after function completes and get returned value

  22. 22

    how to run multiple jobs one after another in jenkins

  23. 23

    How to run multiple python file in a folder one after another

  24. 24

    How do i run a for loop in bash one after another

  25. 25

    How to load 2 Javascript files async and run one after another?

  26. 26

    How to run one statement after another in a procedural manner in Javascript?

  27. 27

    How to run function after reload to another page from special link?

  28. 28

    How to run php function after redirecting user to another page?

  29. 29

    How to redirect to another page after AJAX request completes

HotTag

Archive