Bug Tracker

Opened 14 years ago

Closed 14 years ago

Last modified 9 years ago

#1043 closed bug (wontfix)

unbind() fails on events not attached by jQuery

Reported by: rob.desbois Owned by:
Priority: minor Milestone: 1.1.3
Component: event Version: 1.1.2
Keywords: unbind Cc:
Blocked by: Blocking:


For events which are specified in the HTML source, unbind() fails to attach them. This behaviour is verified with several event types in Firefox and IE6.0 with the following test page:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
   <meta http-equiv='content-type' content='text/html; charset=ISO-8859-1' />
   <script src='/jquery-uncompressed.js' type='text/javascript'></script>
   <script type='text/javascript'><!--
         .html("ready to test");
   function test()
      $("#testButton").unbind("click").html("Tested already");
   // --></script>
   <button id="testButton" onclick="test()">Test me</button>

This can be worked around by using removeAttr() instead:

   $("#testButton").removeAttr("onclick").html("Tested already");

Change History (4)

comment:1 Changed 14 years ago by rob.desbois

Additionally, removeAttr() does not work on events added through jQuery. Thus, if you want to remove the click event from a button but you aren't sure whether it was attached as an onclick attribute or by the jQuery click(fn) function, you must do:


comment:2 Changed 14 years ago by rob.desbois

Open the following script in Fx, click Test. So far, so good. Click Reset then test again. Two alerts?! Click reset once more and it's fine again.

If you trace the events in firebug in a correct test run the following happens: onclick() calls Test(). On the second run when it alerts twice, the first call is Test(), called by onclick(), called by handle(), which is a jQuery function, a jQuery function to 'Handle the second event of a trigger'. That's as far as my diagnosis has gone. Works fine with IE.

   <script src='jquery.js' type='text/javascript'></script>
   <script type='text/javascript'>
   function Test()
   function Reset()
      // shouldn't be required but unbind() doesn't work set in HTML attributes

   <button id='testButton' type='button' onclick='Test()'>Test</button>
   <button type='button' onclick='Reset()'>Reset</button>

comment:3 Changed 14 years ago by brandon

need: Review
Resolution: wontfix
Status: newclosed

jQuery will not remove events it did not bind. However I have seen a couple of requests for this. Perhaps it would be best suited as a plugin/extension to jQuery. Maybe called unbindAll().

comment:4 Changed 12 years ago by SineSwiper

If this is going to be marked as wontfix, then I would highly recommend updating the documentation with this discrepancy and point out the removeAttr example. I got caught by this bug/feature, and it took me a while to figure it out.

I would recommend adding this in as part of the code. It just seems oddly inconsistent without it. (I had no idea that jQuery was not using the default triggers for its event actions.)

Note: See TracTickets for help on using tickets.