Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the dropped item's id on drop event jquery

When I drop something to jquery droppable, I want to get the dropped items' id. When I did it like this:

$("#here").droppable({
        tolerance: 'fit',
        accept: ".one",
         drop: function(){
            id = $(this).attr("id");
            alert (id);
        }
    });

it of course alerted the id of the droppable here. How can I select the id of the dropped div?

like image 913
Logan Avatar asked Jun 08 '11 06:06

Logan


2 Answers

Change your drop function to take two arguments: event, ui

function(event,ui){
    var draggable = ui.draggable;
    var id = draggable.attr("id");
}

The draggable that is being dropped is represented by ui.draggable

Found in the jquery ui docs for droppable.

like image 152
joekarl Avatar answered Oct 12 '22 14:10

joekarl


This worked for me:

   $( "#droppable" ).droppable({
          drop: function( event, ui ) {
                var draggableId = ui.draggable.attr("id");
            var droppableId = $(this).attr("id");
          }
        });
      });
like image 30
Arijit Avatar answered Oct 12 '22 13:10

Arijit