I’m quite new to JQuery but I managed to do some things on my own. I have a three input fields and three options. The options can be dragged into the input fields but there can’t be two draggables into one droppable.
This works perfectly if you don’t move the draggables over the droppables. But when you go over a droppable JQuery executes the “out” event. I was hoping for a “dropout” event which can solve my problem but there isn’t one.
(There is also a problem with the “removeClass” function, because it doesn’t work. However that isn’t a big issue…).
$(function() {
var textbox;
$( ".draggable" ).draggable({
revert: function ( event, ui ) {
$(this).data("draggable").originalPosition = {
top: 0,
left: 0
};
return !event;
},
});
$( ".droppable" ).droppable({
activeClass: "ui-state-hover",
hoverClass: "ui-state-active",
drop: function( event, ui ) {
//check if droppabele contains draggable
if ($(this).data("containsDrop") === 0 || $(this).data("containsDrop") === undefined) { //doesn't contain
$(this).data("containsDrop", 1);
ui.draggable.position( { of: $(this), my: 'left top', at: 'left top' } );
textbox = this;
$( this ).addClass( "ui-state-highlight" )
} else { //contains --> go back to options
ui.draggable.animate({ top: 0, left: 0 }, 'slow');
}
},
out: function ( event, ui ) {
$(this).data("containsDrop", 0);
$(textbox).removeClass( "ui-state-highlight" );
}
});
});
I was hoping that some can take a look to at this Fiddle: http://jsfiddle.net/u7aJ7/10/
Thanks in advance.
I spend a few hours on it and worked out the following solution.
You can check a working example on http://jsfiddle.net/s5057285/yx3gW/