Skip to main content

Bug Tracker

Side navigation

Ticket #964: event.diff
File event.diff, 2.0 KB (added by brandon, February 23, 2007 07:36PM UTC)

Patch and test

Index: jquery/src/event/event.js
===================================================================
--- jquery/src/event/event.js	(revision 1408)
+++ jquery/src/event/event.js	(working copy)
@@ -55,18 +55,36 @@
 
 	// Detach an event or set of events from an element
 	remove: function(element, type, handler) {
-		if (element.$events)
-			if ( type && type.type )
-				delete element.$events[ type.type ][ type.handler.guid ];
-			else if (type && element.$events[type])
+		if (element.$events) {
+			var i,j,k;
+			if ( type && type.type ) { // type is actually an event object here
+				handler = type.handler;
+				type    = type.type;
+			}
+			
+			if (type && element.$events[type])
+				// remove the given handler for the given type
 				if ( handler )
 					delete element.$events[type][handler.guid];
+					
+				// remove all handlers for the given type
 				else
-					for ( var i in element.$events[type] )
+					for ( i in element.$events[type] )
 						delete element.$events[type][i];
+						
+			// remove all handlers		
 			else
-				for ( var j in element.$events )
+				for ( j in element.$events )
 					this.remove( element, j );
+			
+			// remove event handler if no more handlers exist
+			for ( k in element.$events[type] )
+				if (k) {
+					k = true;
+					break;
+				}
+			if (!k) element["on" + type] = null;
+		}
 	},
 
 	trigger: function(type, data, element) {
Index: jquery/src/event/eventTest.js
===================================================================
--- jquery/src/event/eventTest.js	(revision 1408)
+++ jquery/src/event/eventTest.js	(working copy)
@@ -15,7 +15,7 @@
 });
 
 test("unbind(event)", function() {
-	expect(3);
+	expect(4);
 	var el = $("#firstp");
 	el.click(function() {
 		ok( true, "Fake normal bind" );
@@ -25,6 +25,10 @@
 		ok( true, "Fake onebind" );
 	});
 	el.click().click();
+	
+	el.click(function() { return; });
+	el.unbind('click');
+	ok( !el[0].onclick, "Handler is removed" ); // Bug #964
 });
 
 test("trigger(event, [data]", function() {

Download in other formats:

Original Format