I have two functions hooked on the submit
event of a form. Each function is in a different place, and one function can affect the other.
How can I force one of these functions to be hooked with the lowest priority (ie. be the last to be executed)?
There are four ways I can think of:
Manage the callbacks yourself, and only have one event handler that calls the functions in the desired order.
In the function that is supposed to fired last, do the actual work in a zero-millisecond timeout. If all other functions work synchronously (and you can live with the final one not happening during the event bubbling), this will achieve the same thing.
Bind the to-be-fired-last handler higher up in the DOM tree. You'll have to test if that works with submit
events; bubbling of these events doesn't work in IE, but the jQuery docs mention that this has been normalized in jQuery. May be worth a try.
Somewhat similar to 1., when binding any other handler than the low-priority one, unbind the latter, bind the new one, and finally re-bind the last-to-be-run.
You could use custom events for the handlers and have the first handler capture the submit to prevent it, do its stuff, fire the event for the second handler and have the second do the submit
If you want to use / can use custom events, you can use https://github.com/fusion-events/fusion-framework
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With