Skip to main content

Bug Tracker

Side navigation

#11758 closed enhancement (worksforme)

Opened May 12, 2012 11:57AM UTC

Closed May 12, 2012 12:12PM UTC

Last modified May 12, 2012 12:55PM UTC

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:
Blocked by: Blocking:
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.

Attachments (0)
Change History (4)

Changed May 12, 2012 12:12PM UTC by dmethvin comment:1

resolution: → worksforme
status: newclosed

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/

Changed May 12, 2012 12:16PM UTC by dmethvin comment:2

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.***

Changed May 12, 2012 12:53PM UTC by chestozo comment:3

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

Changed May 12, 2012 12:55PM UTC by dmethvin comment:4

Sorry but it doesn't make sense to add and document a disableStandardEventBehavior option for such an edge case. Use your own flags, for example a .data() item on the element.