The other answers are exactly what I was looking for. However, I want to go into a bit further detail here to give a better example of how to process the logic.
Lets say for example, with have some simple HTML as follows. This HTML basically has 4 draggable objects, and 4 possible drop targets:
<div style="margin-bottom:20px;">
<div data-id="1" class="DragItem">I am 1</div>
<div data-id="2" class="DragItem">I am 2</div>
<div data-id="3" class="DragItem">I am 3</div>
<div data-id="4" class="DragItem">I am 4</div>
</div>
<div>
<div data-id="123" class="DropItem">I accept 1, 2 and 3</div>
<div data-id="23" class="DropItem">I accept 2 and 3</div>
<div data-id="34" class="DropItem">I accept 3 and 4</div>
<div data-id="1234" class="DropItem">I accept all</div>
</div>
As can be seen, I have used data-*
attributes to store specific identifying values. The IDs on the DragItem identify the drag object, and the IDs on the DropItem contain all valid values.
The javascript that processes this logic, and then applies the correct classes is as follows:
$(".DragItem").draggable({
revert: true,
helper: "clone"
});
$(".DropItem").droppable({
tolerance: "touch",
over: function (event, ui) {
var dropItem = $(this);
var dragItem = $(ui.draggable);
var valid = String(dropItem.data("id")).indexOf(dragItem.data("id")) > -1;
if (valid) {
dropItem.addClass("DropTargetValid");
} else {
dropItem.addClass("DropTargetInvalid");
}
},
out: function (event, ui) {
var dropItem = $(this);
dropItem.removeClass("DropTargetValid");
dropItem.removeClass("DropTargetInvalid");
},
deactivate: function (event, ui) {
var dropItem = $(this);
dropItem.removeClass("DropTargetValid");
dropItem.removeClass("DropTargetInvalid");
}
});
As can be seen, I am doing a simple "does string contain" logic check. This is fine for small numbers but if there is ever a need to do more than 9 object we would need a more reliable string in the DropItem data-id
value.
I also use the out
and deactivate
events to clear up the applied classes. In this example I duplicate the code, but this could easily be replaced with a single function used by both events.
Finally, the moment you have all been waiting for, here is a working example.