Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery DnD. How to pass data associated with an element being dragged?

There is a list / a table of items (I believe it doesn't matter now). They are made draggable with use of jQuery. I need to set some data associated with an item element being dragged and to be able to extract it from event in "drop" handler of droppable element.

I was trying to use dataTransfer property with its setData() function but the property is not available on both sides.

Here is simplefied example: http://jsfiddle.net/x52ue9ae/1/

like image 748
humkins Avatar asked Jan 18 '26 05:01

humkins


1 Answers

The problem is that this is not HTML5 drag-n-drop, jQueryUI implements pure javascript drag and drop using mousedown/mousemove/mouseup events. There are no dataTransfer mechanism involved at all.

Instead you should set data property using jQuery to the drag-element:

$(function(){
    $(".draggable").draggable({
        start: function(e) {
            $(this).data('id', 3);
        }
    });
    $(".droppable").droppable({
        drop: function(e, ui) {
            var id = ui.draggable.data('id');
            console.log(id);
        }
    })
});

Demo: http://jsfiddle.net/x52ue9ae/2/

like image 153
dfsq Avatar answered Jan 20 '26 19:01

dfsq



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!