Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery event priority

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)?

like image 501
Alex Avatar asked Mar 23 '12 16:03

Alex


3 Answers

There are four ways I can think of:

  1. Manage the callbacks yourself, and only have one event handler that calls the functions in the desired order.

  2. 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.

  3. 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.

  4. 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.

like image 176
balpha Avatar answered Oct 13 '22 23:10

balpha


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

like image 37
Mark Schultheiss Avatar answered Oct 13 '22 23:10

Mark Schultheiss


If you want to use / can use custom events, you can use https://github.com/fusion-events/fusion-framework

like image 39
Ascherer Avatar answered Oct 14 '22 00:10

Ascherer