So, after playing with your code, here is the conclusion I came to.
It seems that the jqueryui drag and drop method tracks its objects separately. When you clone, this tracking is not cloned. I changed your code as follows:
jQuery.fn.swapWith = function(to) { return this.each(function() { var copy_to = $(to).clone(true).appendTo($("#wrapper")); var copy_from = $(this).clone(true).appendTo($("#wrapper"));
You can see the fascinating results http://jsfiddle.net/XkUDv/16/
As you can see, if you drag new cloned objects, it moves the original, not the cloned one.
This is not an answer, but I hope this helps.
UPDATE:
after taking a closer look at the clone, I changed javascript to:
<script type="text/javascript"> jQuery.fn.swapWith = function(to) { return this.each(function() { var copy_to = $(to).clone(); var copy_from = $(this).clone(); $(to).replaceWith(copy_from); $(this).replaceWith(copy_to); }); }; $(document).ready(function() { options = { revert: true }; $("li").draggable(options); $('#wrapper').droppable({ drop: function(event, ui) { window.setTimeout(function(){ $('#one').swapWith($('#two')); $("li").draggable(options); }, 600); } }); }); </script>
Now it works the way you wanted :)
I assume the problem is that since clone (true) copies the event handlers when you try to re-bind the draggable to new clones, it sees the old event handlers and ignores the elements. So instead of clone (true), I changed it to clone ();
hope this helps!
Working version: http://jsfiddle.net/XkUDv/21/
ehudokai
source share