Ticket #9959 (closed bug: invalid)
$.fn.unbind("eventtype", null) unbinds all handlers with type "eventtype"
| Reported by: | kpozin@… | Owned by: | kpozin@… |
|---|---|---|---|
| Priority: | low | Milestone: | None |
| Component: | event | Version: | 1.6.2 |
| Keywords: | Cc: | ||
| Blocking: | Blocked by: |
Description
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).
Change History
comment:2 Changed 23 months ago by dmethvin
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.
comment:3 Changed 23 months ago by dmethvin
- Owner set to kpozin@…
- Status changed from new to pending
comment:4 Changed 23 months ago by kpozin@…
- Status changed from pending to new
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.
comment:5 Changed 23 months ago by rwaldron
- Priority changed from undecided to low
- Resolution set to invalid
- Status changed from new to closed
- Component changed from unfiled to event
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;
Please follow the bug reporting guidlines and use jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

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;