Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I trigger the Drop event with jQuery UI Droppable without actually dragging and dropping?

I have a droppable with a drop event handler:

$(this).droppable({
  drop:function(){
    console.log('OMG You Dropped It!');
  }
});

I have a draggable:

$(this).draggable();

What I want to do is trigger the drop event handler on the droppable without actually dragging and dropping the draggable. I want to simulate the actual behavior without physically performing the behavior.

I thought something like this would do:

$(droppable).trigger('drop', [draggable]);

Unfortunately, it's not quite that simple. Does anyone know how I can accomplish this?

like image 418
Kappers Avatar asked Jul 06 '10 16:07

Kappers


3 Answers

You should move the code in your drop handler to a separate function.
You can then call the function both in the handler and elsewhere.

like image 167
SLaks Avatar answered Nov 11 '22 19:11

SLaks


You can trigger the function associated with the drop call via the option-method:

$("#droppable").droppable({
        drop: function(event, ui) {
            do stuff }
    });
var drop_function = $("#droppable").droppable.option('drop');
drop_function();

This way you get whatever would happen when dropping something on droppable. Of course you could just execute the function instead of assigning it. It's nonetheless a good idea to assign a function to drop, that you define somewhere else, just for clarities sake.

like image 9
ajmurmann Avatar answered Nov 11 '22 18:11

ajmurmann


As pointed by StuperUser and based on ajmurmann's answer, with the recent versions of jQuery you should do:

$("#droppable").droppable({
    drop: function(event, ui) {
        do stuff }
});
var drop_function = $("#droppable").droppable('option', 'drop');
drop_function();
like image 1
Timothée Jeannin Avatar answered Nov 11 '22 18:11

Timothée Jeannin