0

Is it possible to dictate the order in which jQuery events should be fired?

i.e.

<div class="C1 C2 C3"></div>

$('.C1').click(SomeClickEvent);
$('.C2').click(FirstEvent);
$('.C3').click(LastEvent);

Where FirstEvent should always be fired first and LastEvent should always be fired last. SomeEvent doesn't care when it is called.

The main reason being that FirstEvent may want to prevent other events from firing by returning false

Assume that the order in which the events are bound can't be changed. Also assume that the events are bound to the same DOMElement (or Elements) though not necessarily by the same selector (as above)

Finally assume that the events know nothing of each other, are in entirely different (and possibly unrelated) sections of code.

DJL
  • 2,060
  • 3
  • 20
  • 39
  • [Is this helpful for you??](http://stackoverflow.com/questions/18482470/how-to-get-true-false-from-on/18482662#18482662) – Suresh Atta Aug 28 '13 at 09:57
  • http://stackoverflow.com/questions/2360655/jquery-event-handlers-always-execute-in-order-they-were-bound-any-way-around-t – Arun P Johny Aug 28 '13 at 09:58
  • Indeed, they are multiple [discutions](http://stackoverflow.com/questions/290254/how-to-order-events-bound-with-jquery?rq=1) about it. – Brewal Aug 28 '13 at 10:00
  • It seems like all these solutions are dangerous looking hacks that rely on the internals of jQuery - or don't actually address the issue :( – DJL Aug 28 '13 at 10:18

1 Answers1

0

By using the little-known .when() method, you can force certain events to occur in a specific order and base the end result on each individual result.

$.when($('.C1').click(), $('.C2').click())
  .then($('.C2').click(), clickError);

This will fire off both the C1 click and C2 click, and then evaluate their responses. If they both succees {return true}, then the C2 clcik event is triggrered, else clickError is executed.

DevlshOne
  • 8,357
  • 1
  • 29
  • 37