Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drop event not firing in chrome

It seems the drop event is not triggering when I would expect.

I assume that the drop event fires when an element that is being dragged is releases above the target element, but this doesn't seem to the the case.

What am I misunderstanding?

http://jsfiddle.net/LntTL/

$('.drop').on('drop dragdrop',function(){
    alert('dropped');
});
$('.drop').on('dragenter',function(){
    $(this).html('drop now').css('background','blue');
})
$('.drop').on('dragleave',function(){
    $(this).html('drop here').css('background','red');
})
like image 759
Mild Fuzz Avatar asked Jan 24 '14 18:01

Mild Fuzz


3 Answers

In order to have the drop event occur on a div element, you must cancel the ondragenter and ondragover events. Using jquery and your code provided...

$('.drop').on('drop dragdrop',function(){
    alert('dropped');
});
$('.drop').on('dragenter',function(event){
    event.preventDefault();
    $(this).html('drop now').css('background','blue');
})
$('.drop').on('dragleave',function(){
    $(this).html('drop here').css('background','red');
})
$('.drop').on('dragover',function(event){
    event.preventDefault();
})

For more information, check out the MDN page.

like image 151
iamchris Avatar answered Oct 16 '22 03:10

iamchris


You can get away with just doing an event.preventDefault() on the dragover event. Doing this will fire the drop event.

like image 21
Michael Falck Wedelgård Avatar answered Oct 16 '22 02:10

Michael Falck Wedelgård


In order for the drop event to fire, you need to assign a dropEffect during the over event, otherwise the ondrop event will never get triggered:

$('.drop').on('dragover',function(event){
    event.preventDefault();
    event.dataTransfer.dropEffect = 'copy';  // required to enable drop on DIV
})
// Value for dropEffect can be one of: move, copy, link or none
// The mouse icon + behavior will change accordingly.
like image 11
bob Avatar answered Oct 16 '22 03:10

bob