Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery UI - Append Draggable to Droppable

How do you append an item being dragged to a target element on drop, using jQueryUI's draggables/dropables? It looks like the way jQuery UI works right now is it just moves the items around on the screen via absolute positioning. Unfortunately, this functionality makes form submissions useless, as you cannot get the positions of the values on submission.

Thanks in advance for any items/pointers!

like image 379
user1110302 Avatar asked May 22 '12 23:05

user1110302


1 Answers

If I understood correctly, you want the dragged element to be detached from it's current parent and be appended to the new one, right? You can use a helper to do the dragging (so the original element is not affected) and, upon drop, detach it and append it to the target (thanks @Oleg and @Brian for improving over my original answer).

$(myDraggable).draggable({
    helper:"clone",
    containment:"document"
});

$(myDroppable).droppable({
    drop:function(event, ui) {
        ui.draggable.detach().appendTo($(this));
    }
});

​ Working example at jsFiddle

like image 199
mgibsonbr Avatar answered Sep 18 '22 15:09

mgibsonbr