views:

3265

answers:

5
+1  Q: 

clone node on drag

i want to be able to create a copy of the element that i want to drag. im using the standard ui draggable and droppable. i know about the helper clone option. but that does not create a copy. the dragged item gets reverted back to the original position.

+6  A: 

Mark,

Try this example:

     $(document).ready(function(){
  $(".objectDrag").draggable({helper:'clone'});  

  $("#garbageCollector").droppable({
   accept: ".objectDrag",
   drop: function(event,ui){
     console.log("Item was Dropped");
     $(this).append($(ui.draggable).clone());
    }
  });

 });

And the Html looks like this

     <div class="objectDrag" 
  style="width:10%; color:white;border:black 1px solid; background-color:#00A">Drag me</div>

 <div id="garbageCollector" style="width:100%; height:400px; background-color:#333; color:white;"> Drop items on me</div>
Scott
Scott,thanks a lot for this.But I want the cloned/dropped element to be in the same position it was dropped. do you know how i can do it? i tried to add .css(ui.position). but it did not work
mark
Mark, my first guess would've been to use the .css(ui.position), but if you've tried that... What you could try is to create an temp copy of the draggable object on stop. This should contain the relative position of the object. Append that to the container instead of the object itself. Let me know
Scott
Hell yeah! I found this off of Google and it helped big time. Thanks!
Some Canuck
Excellent! Neat and simple.
NLV
A: 

This was very helpful, but I can't redrag the original after. Any ideas?

Mark
A: 

Thanks for your solution. But can I know hw it improve to the clone for multiple times? Sadee

Sadee
A: 

I don't know if this will help but with this guys example you can drag and drop the text may times. http://skfox.com/2008/11/26/jquery-example-inserting-text-with-drag-n-drop/

dburmeister
A: 

To redrag the clone/copy use $(this).append($(ui.draggable).clone(true));

parampadam