Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ondragstart event with jQuery

Tags:

jquery

I've been trying to use Javascript Drag and Drop API.

var file = document.getElementById('file');
file.addEventListener('dragstart', function(evt){
  evt.dataTransfer.setData("DownloadURL",fileDetails);
}, false);

That works and all, but is there any way to use jQuery "on" to catch the event? I've tried

$('#file').on('dragstart', function(evt) {
   console.log(evt.dataTransfer);                    // this returns undefined
   evt.dataTransfer.data("DownloadURL",fileDetails); // so this produces error
});

But it doesn't work.

If possible, I don't want to use jQuery draggable plugin, since I'm not really looking to have a cross-browser solution.

Thanks

like image 242
Henson Avatar asked Jul 17 '13 10:07

Henson


People also ask

How do you trigger a Dragstart event?

The dragstart event is fired when the user starts dragging an element or text selection.

What is a Dragstart event?

The ondragstart event occurs when the user starts to drag an element or text selection. Drag and drop is a very common feature in HTML5. It is when you "grab" an object and drag it to a different location.

Which event is triggered when a draggable object is moved inside an element?

The drag event is fired every few hundred milliseconds as an element or text selection is being dragged by the user.


1 Answers

You have to use the property originalEvent of the jquery event to access native properties of the javascript listener, like this:

evt.originalEvent.dataTransfer
like image 194
A. Wolff Avatar answered Sep 20 '22 12:09

A. Wolff