4

I made a quick drag and drop type menu. Basically I made it so you drag an item from a list into the trash div, and it will give you an alert saying "gone"

I want to make it so that you can't just drag the item anywhere. It has to go into the trash or ".list4" it's called, or else send it back to it's original position.

Here is the JSFiddle: http://jsfiddle.net/Gdze8/

Here is the Javascript:

$( init )

function init() {
$(".contentItem").draggable();
$(".list4").droppable( {
    drop: handleDropEvent
});
}

function handleDropEvent ( event, ui ) {
    var draggable = ui.draggable;
    alert("Gone")
}

Also, while I'm here, would there be a way to delete the item once it goes in the "trash"?

Bradley Mitchell
  • 277
  • 2
  • 5
  • 14

2 Answers2

3

Try this

$(".contentItem").draggable({ revert: 'invalid' });

JS Fiddle Demo

Sachin
  • 40,216
  • 7
  • 90
  • 102
3

use :

   $( ".draggable" ).draggable({ revert: "invalid" }); 

to get it back to its original position.

use:

   $( "#dorp" ).droppable({
                           accept: ".draggable",                                              
                                           drop: function( event, ui ) { ui.draggable.remove(); }
                          });

to remove an element dropped.

HERE IS A DEMO : jsfiddle

AND YOU CAN USE { helper: "clone" } : jsfiddle2

Shlomi Hassid
  • 6,500
  • 3
  • 27
  • 48