views:

16667

answers:

4

Hi, I am using the JQuery libs to implement drag and drop.

How do I get at the element that is being dragged when it is dropped?

I want to get the id of the image inside the div. The following element is dragged:

I have the standard dropped function from their example

$(".drop").droppable({
                 accept: ".block",
                 activeClass: 'droppable-active',
                 hoverClass: 'droppable-hover',
                 drop: function(ev, ui) { }
});

I have tried various ui.id etc which doesnt seem to work.

Thanks,

+12  A: 

Is it not the ui.draggable?

If you go here (in Firefox and assuming you have firebug) and look in the firebug console youll see I am doing a console.dir of the ui.draggable object which is the div being dragged

http://jsbin.com/ixizi

Therefore the code you need in the drop function is

       drop: function(ev, ui) {
                 //to get the id
                 //ui.draggable.attr('id') or ui.draggable.get(0).id or ui.draggable[0].id
                 console.dir(ui.draggable)  
       }
redsquare
Thanks - I get console not defined in firebug?
alexmac
alexmac - replace the console.dir with a debugger; statement, u can then view the ui.draggable property in the watch window.Not sure why it says not defined. What version of FB and FF? The dir method is standard for FB see http://getfirebug.com/console.html
redsquare
Just before console you needwindow.loadFirebugConsole();to get the console to work.
MDCore
@MDCore - Never used or seen that and never had any issues myself. I assume that is when you have FB turned off and need to enable it. I just assume all web devs have it on.
redsquare
I had an old version of firefox (2 something) the console works now
alexmac
+3  A: 

redquare is right, inside your function refer to ui.draggable:

$(".drop").droppable({ accept: ".block", 
                       activeClass: 'droppable-active', 
                       hoverClass: 'droppable-hover', 
                       drop: function(ev, ui) { 
                           //do something with ui.draggable here
                       }
});

That property points to the thing being dragged.

Note that if you're using cloned "helpers", the draggable will be the cloned copy, not the original.

Adam Bellaire
ui.draggable is an object but I dont seem to be able to get the div which is the draggable element. ui.draggable.id or ui.draggable.innerHTML does not work.
alexmac
ui.draggable.attr('id') or ui.draggable.get(0).id
redsquare
+1  A: 

$(ui.draggable).attr("id")

+1 for BullsEye!
Mohit Nanda
A: 

Does anyone know where I can find a list of parameters included with various jQueryUI callbacks and events? Their site - for what I can tell - has little to no details on this.

Ryan Thompson