I have a widget in which I can drop items into a container. I want to be able to leave the original element outside of the droppable container, so that the user can drag and drop the same element into the container (so the container can have multiple of the same element). I am using the clone helper, but can’t figure out how to not delete the original element. Any help is appreciated. Thanks! Below is my code:
$(function() {
var $gallery = $( "#gallery" ),
$trash = $( "#trash" );
$( "li", $gallery ).draggable({
cancel: "a.ui-icon",
revert: "invalid",
containment: $( "#demo-frame" ).length ? "#demo-frame" : "document", // stick to demo-frame if present
helper: "clone",
cursor: "move"
});
$trash.droppable({
accept: "#gallery > li",
activeClass: "ui-state-highlight",
drop: function( event, ui ) {
deleteImage( ui.draggable );
}
});
$gallery.droppable({
accept: "#trash li",
activeClass: "custom-state-active",
drop: function( event, ui ) {
recycleImage( ui.draggable );
}
});
HTML:
<div class="demo ui-widget ui-helper-clearfix">
<ul id="gallery" class="gallery ui-helper-reset ui-helper-clearfix">
<li class="ui-widget-content ui-corner-tr" a href="link/to/trash/script/when/we/have/js/off">
<h5 class="fpheader">Red</h5>
</li>
<li class="ui-widget-content ui-corner-tr">
<h5 class="fpheader">Orange</h5>
</li>
<li class="ui-widget-content ui-corner-tr">
<h5 class="fpheader"Yellow</h5>
</li>
<li class="ui-widget-content ui-corner-tr">
<h5 class="fpheader">Green</h5>
</li>
<li class="ui-widget-content ui-corner-tr">
<h5 class="fpheaderr">Blue</h5>
</li>
<li class="ui-widget-content ui-corner-tr">
<h5 class="fpheader">Purple</h5>
</li>
<li class="ui-widget-content ui-corner-tr">
<h5 class="fpheader">White</h5>
</li>
</ul>
</div>
The clone helper clones the element you’re dragging for the purpose of display while dragging. Otherwise it does not clone the element on drop. What you need to do inside your drop handler is to clone the element yourself (you can use
ui.draggable) and attach it to your droppable container.Here’s an example: