I am using the JQuery UI droppable library features, and I want to provide visual user feedback when they hover over a droppable target. For this I can easily use the hoverClass
option to specify which class to use when they have a draggable item hovering over.
But what I want to do is use a different hoverClass
value depending on some logic. Basically, there are a number of areas that are "droppable", and there is a number of items that can be dragged and dropped - however, not all items can be dropped on all areas. So I would like to have, for example, a green background if the drop is valid, and a red background if the drop is invalid.
How can this be done? I know what logic I want to use, but where can I add the logic. It obviously needs to be somewhere I can access the element being dragged, and the potential drop target element.
My simple code so far is as follows:
$(".DragItem").draggable({
revert: true,
helper: "clone"
});
$(".DropItem").droppable({
tolerance: "touch",
hoverClass: "DropTargetValid"
});
$(".DropItem").droppable({
tolerance: "touch",
hoverClass: "DropTargetValid",
over: function(event, ui) {
console.log(ui.draggable); // the draggable object
console.log($(this)); // the droppable object
}
});
This should do it. On over that event will be triggered on all .DropItem elements. You can find more about the available events API here: http://api.jqueryui.com/droppable/