Run a function after an on.change event

nobrandheroes picture nobrandheroes · Mar 27, 2015 · Viewed 10.6k times · Source

I have something like this: $('#select1').on('change', function() {}) and it works fine. However, I need to run another function after the function has completed.

I thought about taking the function contents, and putting it in a named function, then taking the second function and doing the same, and placing them in the anonymous function:

$('#select1').on('change', function() {
    function1 ();
    function2 ();
});

However, I was hoping there was another way. The above seems inconsistent with jQuery.

Answer

Giovanni Perillo picture Giovanni Perillo · Mar 27, 2015

Try this:

$('#select1').on('change', function() {
    function1(someVariable, function() {
      function2(someOtherVariable);
    });
});


function function1(param, callback) {
  //...do stuff
  callback();
} 

Here is where I found that: Call a function after previous function is complete