views:

647

answers:

4

jQuery question

What happens if I bind two event handlers to same event and same element:

For example:

var elem = $("...")
elem.click(...);
elem.click(...);

Later wins or both handlers will be run?

+2  A: 

Both handlers get called.

You may be thinking of inline event binding (eg "onclick=..."), where a big drawback is only one handler may be set for an event.

jQuery conforms to the DOM Level 2 event registration model:

The DOM Event Model allows registration of multiple event listeners on a single EventTarget. To achieve this, event listeners are no longer stored as attribute values

Crescent Fresh
do you know in what order?
Rich Seller
@Rich: the order is officially undefined.
Crescent Fresh
+3  A: 

Both handlers will run, the jQuery event model allows multiple handlers on one element, therefore a later handler does not override an older handler.

I don't believe the order can be determined/trusted, so don't rely on the order.

Russ Cam
+2  A: 

Suppose that you have two handlers, f and g, and want to make sure that they are executed in a known and fixed order, then just encapsulate them:

$("...").click(function(event){
  f(event);
  g(event);
});

In this way there is (from the perspective of jQuery) only one handler, which calls f and g in the specified order.

Stephan202
+1, encapsulation inside a function is the way to go. And even better conditional logic can be contained inside the handler function to control the event(s) that get triggered.
Gordon Potter
A: 

You should be able to use chaining to execute the events in sequence, e.g.:

$('#target')
  .bind('click',function(event) {
    alert('Hello!');
  })
  .bind('click',function(event) {
    alert('Hello again!');
  })
  .bind('click',function(event) {
    alert('Hello yet again!');
  });

I guess the below code is doing the same

$('#target')
      .click(function(event) {
        alert('Hello!');
      })
      .click(function(event) {
        alert('Hello again!');
      })
      .click(function(event) {
        alert('Hello yet again!');
      });

Source: http://www.peachpit.com/articles/article.aspx?p=1371947&seqNum=3

anddoutoi
Neither that code nor that article defines an order of handler execution. Yes, that is the order the event *binding* will occur, but the order the event handlers get called is still officially undefined.
Crescent Fresh
ehh? what about "Now when the click event occurs, the first event handler will be called, followed by the second, followed by the third." is unclear?
anddoutoi
and yes, i know that the official recs do not define this and PPK has already proved that the event execution is random but maybe jQuery has fixed this ^^
anddoutoi
Ahh, missed that sentence. In fact, the author is misinformed. jQuery has not "fixed" this. Because the spec does not formally define the order, technically nothing needs fixing.
Crescent Fresh
Yeah, I find it weird. The same example and code is also in the book jQuery in Action by Bibeault and Katz with foreword by jresig himself. Wish that John could confirm or refute this once in for all.I know your here on SO John so could you? Please ^^
anddoutoi