Skip to main content

Bug Tracker

Side navigation

#10821 closed bug (fixed)

Opened November 17, 2011 08:04PM UTC

Closed November 17, 2011 10:42PM UTC

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

Reported by: gjudd@stayinfront.com Owned by: gjudd@stayinfront.com
Priority: undecided Milestone: None
Component: event Version: 1.6.4
Keywords: Cc:
Blocked by: Blocking:
Description

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:

https://github.com/jquery/jquery/commit/e7912805d6ee290071fb15fbca752e9f47fcd032

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.

Attachments (0)
Change History (6)

Changed November 17, 2011 08:06PM UTC by gjudd@stayinfront.com comment:1

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.

Changed November 17, 2011 08:48PM UTC by rwaldron comment:2

component: unfiledevent
owner: → gjudd@stayinfront.com
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.

Changed November 17, 2011 09:06PM UTC by dmethvin comment:3

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.

Changed November 17, 2011 09:09PM UTC by Gwyn Judd <gjudd@stayinfront.com> comment:4

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/

Changed November 17, 2011 09:13PM UTC by Gwyn Judd <gjudd@stayinfront.com> comment:5

Replying to [comment:3 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.

Changed November 17, 2011 10:42PM UTC by dmethvin comment:6

resolution: → fixed
status: pendingclosed

Documentation updated.