Bug Tracker

Modify

Ticket #10821 (closed bug: fixed)

Opened 18 months ago

Last modified 18 months ago

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

Reported by: gjudd@… Owned by: gjudd@…
Priority: undecided Milestone: None
Component: event Version: 1.6.4
Keywords: Cc:
Blocking: Blocked by:

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.

Change History

comment:1 Changed 18 months ago by gjudd@…

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 18 months ago by rwaldron

  • Owner set to gjudd@…
  • Status changed from new to pending
  • Component changed from unfiled to event

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 follow-up: ↓ 5 Changed 18 months 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 18 months ago by Gwyn Judd <gjudd@…>

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 18 months ago by Gwyn Judd <gjudd@…>

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 18 months ago by dmethvin

  • Status changed from pending to closed
  • Resolution set to fixed

Documentation updated.

Please follow the  bug reporting guidlines and use  jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

View

Add a comment

Modify Ticket

Action
as closed
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.