Example code here: http://jsfiddle.net/pDuxe/
I have a droppable
space with two draggable
elements. I have two issues:
If the droppable
area already has a draggable
item in it, if I then try and drop a second item on top, I need it to be refused
and to revert back to its original position.
I can make use of the revert: 'invalid'
code without problems, but if I place a draggable
item into a droppable
area and then want to move it back - it keeps reverting back to its place in the droppable
area and not its original position.
So, how would I go about achieving those two situations?
What you can do is wrap your draggables in a div
that you also make droppable
:
<div id="originalposition">
<div id="draggable" class="ui-widget-content">
<p>Drag me to my target</p>
</div>
<div id="draggable2" class="ui-widget-content">
<p>Drag me to my target</p>
</div>
</div>
Then you can do something like the following:
$(function() {
$("#draggable").draggable({ revert: 'invalid' });
$("#draggable2").draggable({ revert: 'invalid' });
$("#originalposition").droppable({
drop: function(event,ui) {
$("#droppable").removeClass("ui-state-highlight").addClass("ui-widget-header").html("<p>Drop here</p>");
}
});
$("#droppable").droppable({
drop: function(event, ui) {
$(this).addClass("ui-state-highlight").find("p").html("Dropped!");
},
accept: function(dropElem) {
//dropElem was the dropped element, return true or false to accept/refuse it
return (!$(this).hasClass("ui-state-highlight") && $(dropElem).hasClass("ui-widget-content"));
}
});
});