Skip to main content

Bug Tracker

Side navigation

#6222 closed bug (worksforme)

Opened March 04, 2010 02:59PM UTC

Closed March 04, 2010 07:41PM UTC

Breaking change in bind() when binding to the same handler multiple times

Reported by: roatin.marth@gmail.c Owned by:
Priority: Milestone: 1.4.3
Component: event Version: 1.4.2
Keywords: bind multiple Cc:
Blocked by: Blocking:
Description

Given:

<code>

function foo() { alert('bar') }

$(elem).click(foo);

$(elem).click(foo);

$(elem).click(foo);

</code>

In jQuery 1.3.2, clicking on elem would cause the alert to show only once, as per the addEventListener standard https://developer.mozilla.org/en/DOM/element.addEventListener#Multiple_identical_event_listeners:

"If multiple identical EventListeners are registered on the same EventTarget with the same parameters, the duplicate instances are discarded. They do not cause the EventListener to be called twice..."

In jQuery 1.4.2 clicking on elem will cause the alert to show not once, not twice, but thrice.

Attachments (1)
  • 6222.html (0.4 KB) - added by roatin.marth@gmail.c March 04, 2010 03:04PM UTC.

    Sample breaking test case

Change History (3)

Changed March 04, 2010 03:18PM UTC by john comment:1

resolution: → wontfix
status: newclosed

That was an intentional change in 1.4.2 and was mentioned in the release notes. We made this change because we have multiple ways of binding a handler to an object - a handler with data, a handler with namespaces, a handler with data and namespaces, and even just a plain handler. Considering all these possibilities it ended up making a lot of sense to just allow users to bind events multiple times (especially since they were trying to do so already, in those different contexts, and having serious problems).

Changed March 04, 2010 07:27PM UTC by roatin.marth@gmail.c comment:2

resolution: wontfix
status: closedreopened

Is this what you're referring to? http://blog.jquery.com/2010/02/19/jquery-142-released/:

"It’s now possible to bind identical handlers with different data, namespaces, and event types universally."

If so, it is not obvious from that snippet that if you're using plain ol' DOM event binding (ie no event data, no namespaces), 1.4 will break code that previously worked in 1.3.x.

Every other DOM library does what the spec outlines (ie, discard identical handlers). IE's attachEvent is the only event model that screws it up, and now jQuery 1.4 has "normalized" all the previously working browsers to all behave like IE.

Can this be better documented somewhere?

Changed March 04, 2010 07:41PM UTC by john comment:3

resolution: → worksforme
status: reopenedclosed

I added more detailed information and an example to the bind documentation:

http://api.jquery.com/bind/