Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery draggable/droppable: access to original element

I'm dragging elements from one unordered list to another:

$('ul#drag li').draggable({ helper: 'clone' });
$('ul#drop').droppable({
    drop: function (event, ui) {
        ui.draggable.sourceElement.css(... ...);
    }
});

I want to mark already dragged elements in source list (but still allow dragging them), how do I access them through jQuery chain?

I guess I can set id attribute on the dragged element, and when dropping, the cloned element would have the same id, which I can use in finding the original, but I'm sure there's a nicer solution.


like image 768
Martin Tóth Avatar asked Dec 07 '10 11:12

Martin Tóth


People also ask

How do you use draggable and droppable?

Droppable() Method: This method allows the elements to be dropped with the help of mouse. Using jQuery UI, we can make the DOM(Document Object Model) elements to drop anywhere within the view port on the specified target. This can be done by clicking on the draggable object by mouse and drop it on the specified target.

Why is draggable not working?

You have one of these problems: Your jQuery or jQuery UI Javascript path files are wrong. Your jQuery UI does not include draggable. Your jQuery or jQuery UI Javascript files are corrupted.

What is jQuery draggable?

jQueryUI provides draggable() method to make any DOM element draggable. Once the element is draggable, you can move that element by clicking on it with the mouse and dragging it anywhere within the viewport.


2 Answers

I wonder why I did not notice that the following works, the first time I tried it:

ui.draggable.css('whatever');

It's even documented:

ui.draggable - current draggable element, a jQuery object.

like image 84
Martin Tóth Avatar answered Sep 26 '22 20:09

Martin Tóth


e.target refers to the original element

like image 20
George Avatar answered Sep 26 '22 20:09

George