Bug Tracker

Modify

Ticket #7520 (closed bug: worksforme)

Opened 3 years ago

Last modified 15 months ago

$(document).unbind('click') seems to clear live click callbacks

Reported by: art.orlov Owned by:
Priority: low Milestone:
Component: event Version: 1.4.4
Keywords: neededdocs Cc:
Blocking: Blocked by:

Description

It seems that doing $(document).unbind('click') removes not only all click handlers attached to document node but also live click handlers like: $('a[data-method]:not([data-remote])').live('click', ...)

The same stuff with $('body').delegate() because it uses the same live technique.

It seems clear that $(document).unbind('click') should only clear callbacks attached using bind function. And die is designed for live callbacks.

Change History

comment:1 Changed 3 years ago by jitter

possible  test case

comment:2 Changed 3 years ago by boushley

Dang jitter beat me to it! Here's my fiddle anyways  http://jsfiddle.net/boushley/y5fVv/

comment:3 Changed 3 years ago by jitter

Well I like yours better anyway :D

comment:4 follow-up: ↓ 5 Changed 3 years ago by Neeraj Singh

Not sure if it is valid but I attempted to create a failing test.  https://github.com/neerajdotname/jquery/commit/9cce81f0c19ed3506f0d955716f280b2e60d2fcc

comment:5 in reply to: ↑ 4 Changed 3 years ago by art.orlov

Replying to Neeraj Singh:

Not sure if it is valid but I attempted to create a failing test.  https://github.com/neerajdotname/jquery/commit/9cce81f0c19ed3506f0d955716f280b2e60d2fcc

Seems to be absolutely correct testcase, that's what we did in our code. Thanks for writing it.

Going spend time next time on writing testcases instead of text description :)

comment:6 Changed 3 years ago by rwaldron

  • Keywords needsreview added
  • Priority changed from undecided to low
  • Status changed from new to open
  • Component changed from unfiled to event
  • Milestone 1.5 deleted

Confirmed - but it the behaviour is consistent all the way back to 1.2.6

 http://jsfiddle.net/rwaldron/y5fVv/4/

This is a little clearer, and further reduced. The test result is laid out.

comment:7 Changed 2 years ago by dmethvin

Given the implementation this makes sense; there is a special "live" event attached to the delegate elements (document for the .live() case but any element for .delegate()) and if you unbind all events from an element it, well, unbinds ALL events.

Should we just document it works this way, or somehow segregate the events better? I'd prefer to see it documented because it seems like a lot of work to keep them apart. Long term I think there could be some benefit to unifying our event APIs similar to what Prototype did with .on("event", "selector", data, fn) and if we start treating them differently that will make less sense.

comment:8 Changed 2 years ago by art.orlov

I see that such behavior is not transparent to user. And i guess most of them don't know how .live() event handler works. I'd prefer the way of separating delegated and direct event handlers. Like the way native event handlers (attached via .addEventListener etc.) are separated from ones handled by jQuery.

comment:9 Changed 2 years ago by john

  • Keywords needsdocs added; needsreview removed
  • Status changed from open to closed
  • Resolution set to worksforme

I think that this is "OK" (in that it's the expected result - delegate and live both are attaching click handlers to the document). I don't think we should try to obscure this, that might end up creating unexpected results. I'm going to flag this as "needsdocs" so that we can go back later and improve the documentation here.

comment:10 Changed 2 years ago by addyosmani

Initial docs update:  http://api.jquery.com/unbind/. Could be improved.

comment:11 Changed 2 years ago by addyosmani

  • Keywords neededdocs added; needsdocs removed

Please follow the  bug reporting guidlines and use  jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

View

Add a comment

Modify Ticket

Action
as closed
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.