Bug Tracker

Opened 10 years ago

Closed 10 years ago

#10821 closed bug (fixed)

JQuery.event.add can add handlers for the same event multiple times

Reported by: [email protected] Owned by: [email protected]
Priority: undecided Milestone: None
Component: event Version: 1.6.4
Keywords: Cc:
Blocked by: Blocking:


This bug identifies a change in behaviour between 1.3.2 and 1.6.4. The change appears to have been introduced with this refactoring:


Prior to this refactoring, the handlers array was defined as an object, and handlers were added to this array keyed on the guid:

handlers = events[ type ] = {}; ... handlers[ handler.guid ] = handler;

Subsequent to the change:

handlers = events[ type ] = []; ... handlers.push( handleObj );

Obviously the first method has the effect of ensuring that the same event will not ever be added twice to the handlers list.

Change History (6)

comment:1 Changed 10 years ago by [email protected]

Here is a minimal page for replicating the issue:

<!DOCTYPE html> <html> <head>

<title>Untitled Page</title> <script src="ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script>

</head> <body> <li>click</li> <script> function clickit() { alert('clicked'); } $('li').click(clickit); $('li').click(clickit); </script> </body> </html>

when you click the li, you will see that the alert appears only one time. If you change the jquery include to 1.6.4 or 1.7.0, then it will appear two times.

comment:2 Changed 10 years ago by Rick Waldron

Component: unfiledevent
Owner: set to [email protected]
Status: newpending

Thanks for taking the time to contribute to the jQuery project! Please provide a complete reduced test case on jsFiddle to help us assess your ticket!

Additionally, be sure to test against the "jQuery (edge)" version to ensure the issue still exists. To get you started, use this boilerplate: http://jsfiddle.net/FrKyN/ Open the link and click to "Fork" (in the top menu) to get started.

comment:3 Changed 10 years ago by dmethvin

It's documented at http://api.jquery.com/on/ :

The same event handler can be bound to an element multiple times. This is especially useful when the event.data feature is being used, or when other unique data resides in a closure around the event handler function. For example:

 function greet(event) { alert("Hello "+event.data.name); }
$("button").on("click", { name: "Karl" }, greet);
$("button").on("click", { name: "Addy" }, greet);

The above code will generate two different alerts when the button is clicked.

The only action item I see is to document that this changed as of 1.4, but that was almost two years ago.

comment:4 Changed 10 years ago by Gwyn Judd <[email protected]…>

I have updated the jsFiddle link you provided with working test code (I think). I haven't used jsFiddle before so my apologies if I did something incorrect.

The new link should be: http://jsfiddle.net/FrKyN/126/

comment:5 in reply to:  3 Changed 10 years ago by Gwyn Judd <[email protected]…>

Replying to dmethvin:

The only action item I see is to document that this changed as of 1.4, but that was almost two years ago.

As a user, I hope you will do so - knowing about changes in behaviour from release to release is extremely high priority. I want to keep my code base up to date with later releases, but the risk of finding unexpected defects can prevent us from doing so.

comment:6 Changed 10 years ago by dmethvin

Resolution: fixed
Status: pendingclosed

Documentation updated.

Note: See TracTickets for help on using tickets.