Ticket #11758 (closed enhancement: worksforme)
off(): $(document).off() does not disable event handlers, binded via another $(document).on() handler
| Reported by: | chestozo | Owned by: | |
|---|---|---|---|
| Priority: | undecided | Milestone: | None |
| Component: | unfiled | Version: | 1.7.2 |
| Keywords: | Cc: | ||
| Blocking: | Blocked by: |
Description
Here I wrote a little demo for the bug: http://jsbin.com/ofejov/edit#source
I know, that this is not really a bug, better called - it is a design issue. When someone uses off() he is not able to disable any handler that is already in events queue because events queue for delegate and live events is composed before real handlers are called, here: https://github.com/jquery/jquery/blob/master/src/event.js#L447
And if some handler off() method is called - queue is not modifed and this allows "disabled" handlers to be triggered.
Change History
comment:1 Changed 13 months ago by dmethvin
- Status changed from new to closed
- Resolution set to worksforme
comment:2 Changed 13 months ago by dmethvin
Just to quote some spec:
http://www.w3.org/TR/DOM-Level-3-Events/#event-flow
Next, the implementation must determine the current target's candidate event listeners. This must be the list of all event listeners that have been registered on the current target in their order of registration. [HTML5] defines the ordering of listeners registered through event handler attributes. Once determined, the candidate event listeners cannot be changed; adding or removing listeners does not affect the current target's candidate event listeners.*
comment:3 Changed 13 months ago by chestozo
Yes, works like in specs, thank you.
Maybe, it would be great to have some force option to force event prevention by .off() method. Because in some large applications it can be difficult to analyze, "why am I calling off() method and it does not work?".
In my real example I cannot disable all click events by calling stopImmediatePropagation() - someone may be interested in this click. All I want - is to disable some specific handler. Like here: http://jsbin.com/ofejov/3/edit
Please follow the bug reporting guidlines and use jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

You've just attached two event handlers to document there, of course they'll both run. The names you've given the handler functions have no bearing on anything. If you only want the first to run, use evt.stopImmediatePropagation().
http://jsbin.com/ofejov/2/