Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bind native html5 dragstart event handler and access dataTransfer using jQuery

This is working:

$(".my_dragging_class").each( makeDraggable($(this).get()[0]) );

var makeDraggable = function (el){
    el.draggable = 'true';
    el.addEventListener('dragstart', function(e){
                e.dataTransfer.effectAllowed = 'move';
                e.dataTransfer.setData('text/html', 'test');
                console.log('dragstart');
                }, 
            false);
}

This is NOT working:

$(".my_dragging_class").makeDraggable();

$.fn.makeDraggable(){
    $(this).attr('draggable','true');
    $(this).bind('dragstart', function(e){
                e.dataTransfer.effectAllowed = 'move';
                e.dataTransfer.setData('text/html', 'test');
                console.log('dragstart');
                }, 
            false);
}

Error message: I receive a 'e.dataTransfer is undefined' for the line e.dataTransfer.effectAllowed = 'move';. My Browser is FireFox 10.0.2

Is it possible to use dataTransfer with jQuery this way?

like image 507
Simon Avatar asked Mar 05 '12 12:03

Simon


2 Answers

Try:

$.fn.makeDraggable(){
    $(this).attr('draggable','true');
    $(this).bind('dragstart', function(e){
                e.originalEvent.dataTransfer.effectAllowed = 'move';
                e.originalEvent.dataTransfer.setData('text/html', 'test');
                console.log('dragstart');
                }, 
            false);
}

The jQuery event object does not have a dataTransfer property.

like image 74
robertc Avatar answered Nov 13 '22 00:11

robertc


The jQuery event object does not have a dataTransfer property... true, but one can try:

jQuery.event.props.push('dataTransfer');

kr, zara

like image 33
user1875278 Avatar answered Nov 13 '22 01:11

user1875278