javascriptjqueryjquery-ui-droppablejquery-droppable

JQuery multiple droppables and one function to handle each


Being new to JavaScript/JQuery I'm having a bit of a problem figuring out how could I implement registering drop function of multiple droppables to one function, for example:

$('#droppableLW').droppable({
    drop : handleUIDropEvent
});

$('#droppableRW').droppable({
    drop : handleUIDropEvent
});

And obtaining the id of droppable inside handleUIDropEvent method, for instance to achieve this:

function handleUIDropEvent(event, ui) {
   var droppableId = somehowGottaGetIt;
   if (new String(droppableId).valueOf() == new String("droppableLW").valueOf()) {
      ...
   } else {
      ...
   }

}

Solution

  • To do that just use the class attribute instead the id

    $('.droppableObject').droppable({
        drop : function( event, ui ) {
            if ( this.id == "something" ) {
                // do things
            }
        }
    });