Reputation: 4539
I am just getting started with this, but I am wondering how could I take the very simple example below and modify so that if the draggable div is already placed on the droppable div it won't fire the alert.
<script type="text/javascript">
$(function () {
$("#draggable-1").draggable();
$("#draggable-2").draggable();
$("#droppable").droppable({
drop: function (event, ui) {
var currentId = $(ui.draggable).attr('id');
if (currentId == "draggable-1") {
$(this)
// would like to prevent this if draggable is already dropped!
alert("Adding Item #1.")
} else {
$(this)
alert("Adding Item #2.")
}
}
});
});
</script>
<table width="100%"><tr><th>Draggable</th><th>Order Section</th><tr><td>
<div id="draggable-1">
<p>Item #1</p>
</div>
<div id="draggable-2">
<p>Item #2.</p>
</div>
<div id="droppable">
<p>Add Your Item</p>
</div>
Upvotes: 1
Views: 118
Reputation: 5621
Check this fiddle: http://jsfiddle.net/100thGear/XfUvS/
A bit buggy with the positions, but achieves the desired effect. Let me know if this helps!
Upvotes: 1
Reputation: 5494
One solution that worked for me:
var wasInDrop = false;
var finishInDrop = false;
$("#draggable_1").draggable({
start: function(event, ui){
if (! finishInDrop){
wasInDrop = false;
}
finishInDrop = false; //will be set to true if landing in drop.
},
stop: function(event, ui){
if (finishInDrop && !wasInDrop){
alert("Adding Item #1.");
}
if (finishInDrop){ wasInDrop=true;}
}
});
$("#droppable").droppable({
drop: function (event, ui) {
$(this)
var currentId = $(ui.draggable).attr('id');
if (currentId == "draggable_1") {
finishInDrop = true; //for this time.
}
});
And so forth. The idea is that the draggable fires the alert, if:
Upvotes: 1