Skip to main content

Bug Tracker

Side navigation

Ticket #921: jquery.clone.diff
File jquery.clone.diff, 1.2 KB (added by brandon, May 14, 2007 05:39PM UTC)

This patch fixes it all.

Index: src/jquery/jquery.js
===================================================================
--- src/jquery/jquery.js	(revision 1897)
+++ src/jquery/jquery.js	(working copy)
@@ -852,11 +852,30 @@
 	 * @cat DOM/Manipulation
 	 */
 	clone: function(deep) {
-		return this.pushStack( jQuery.map( this, function(a){
-			a = a.cloneNode( deep != undefined ? deep : true );
-			a.$events = null; // drop $events expando to avoid firing incorrect events
-			return a;
+		// Need to remove events on the element and its descendants
+		var $this = this.add(this.find("*"));
+		$this.each(function() {
+			this._$events = {};
+			for (var type in this.$events)
+				this._$events[type] = jQuery.extend({},this.$events[type]);
+		}).unbind();
+
+		// Do the clone
+		var r = this.pushStack( jQuery.map( this, function(a){
+			return a.cloneNode( deep != undefined ? deep : true );
 		}) );
+
+		// Add the events back to the original and its descendants
+		$this.each(function() {
+			var events = this._$events;
+			for (var type in events)
+				for (var handler in events[type])
+					jQuery.event.add(this, type, events[type][handler], events[type][handler].data);
+			this._$events = null;
+		});
+
+		// Return the cloned set
+		return r;
 	},
 
 	/**

Download in other formats:

Original Format