From baa44a8f2731ec48f5f1b2349db2f956b7173635 Mon Sep 17 00:00:00 2001 From: John Resig Date: Sat, 23 Dec 2006 17:41:00 +0000 Subject: [PATCH] Added in #690, the ability to remove an event handler from inside itself. --- src/jquery/coreTest.js | 13 +++++++++++++ src/jquery/jquery.js | 8 +++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/jquery/coreTest.js b/src/jquery/coreTest.js index ada71959e..67dea206f 100644 --- a/src/jquery/coreTest.js +++ b/src/jquery/coreTest.js @@ -564,3 +564,16 @@ test("removeClass(String) - add three classes and remove again", function() { test("removeAttr(String", function() { ok( $('#mark').removeAttr("class")[0].className == "", "remove class" ); }); + +test("unbind(event)", function() { + expect(3); + var el = $("#firstp"); + el.click(function() { + ok( true, "Fake normal bind" ); + }); + el.click(function(event) { + el.unbind(event); + ok( true, "Fake onebind" ); + }); + el.click().click(); +}); diff --git a/src/jquery/jquery.js b/src/jquery/jquery.js index d22d136f7..63b6ece92 100644 --- a/src/jquery/jquery.js +++ b/src/jquery/jquery.js @@ -2090,7 +2090,9 @@ jQuery.extend({ // Detach an event or set of events from an element remove: function(element, type, handler) { if (element.events) - if (type && element.events[type]) + if ( type && type.type ) + delete element.events[ type.type ][ type.handler.guid ]; + else if (type && element.events[type]) if ( handler ) delete element.events[type][handler.guid]; else @@ -2135,6 +2137,10 @@ jQuery.extend({ args.unshift( event ); for ( var j in c ) { + // Pass in a reference to the handler function itself + // So that we can later remove it + args[0].handler = c[j]; + if ( c[j].apply( this, args ) === false ) { event.preventDefault(); event.stopPropagation();