Skip to main content

Bug Tracker

Side navigation

#9959 closed bug (invalid)

Opened August 02, 2011 08:04PM UTC

Closed August 02, 2011 09:14PM UTC

Last modified August 02, 2011 11:42PM UTC

$.fn.unbind("eventtype", null) unbinds all handlers with type "eventtype"

Reported by: Owned by:
Priority: low Milestone: None
Component: event Version: 1.6.2
Keywords: Cc:
Blocked by: Blocking:

jQuery.event.remove currently unbinds all handlers matching the event type and namespace if ( !handler ). This means that if null is accidentally passed in, all matching handlers will be unbound.

There should be a distinction between the handler argument being absent (using arguments.length or typeof handler === "undefined") and the handler argument not being a function. If the argument is absent, the intention is to unbind all matching events. If the handler argument is present but not a function, nothing should be done (as if nothing matched its guid).

Attachments (0)
Change History (6)

Changed August 02, 2011 08:33PM UTC by danheberden comment:1

I'm confused at the use-case for this. Why would anyone "accidentally" pass in null? If nothing is to be unbound, the function should not be called, ya? Plus if i'm after a boolean option to unbind, i'd rather is be falsy instead of someVar ? undefined : null;

Changed August 02, 2011 08:40PM UTC by dmethvin comment:2

Why are you calling jQuery.event.remove directly anyway? It is not a public API. And if you passed in null it sounds like you have a code error, like danheberden said.

Changed August 02, 2011 08:41PM UTC by dmethvin comment:3

owner: →
status: newpending

Changed August 02, 2011 08:47PM UTC by comment:4

status: pendingnew

Hmm, maybe the fix should be on unbind rather than on remove. Yes, passing in null is probably a code error. But when null is passed as a handler to .bind(), it's handled gracefully (without throwing an exception). For unbinding, I think handling that case gracefully would mean to not unbind all unrelated handlers.

Changed August 02, 2011 09:14PM UTC by rwaldron comment:5

component: unfiledevent
priority: undecidedlow
resolution: → invalid
status: newclosed

I'm not sure why we're even debating this. Passing null as a second arg to unbind is incorrect. The second arg has accepted params: handler or false;

Changed August 02, 2011 11:42PM UTC by dmethvin comment:6

Given the reply, I agree this is invalid.

But when null is passed as a handler to .bind(), it's handled gracefully (without throwing an exception).

That is not documented behavior, I wouldn't depend on it to work that way forever.