Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drop Event Not Preventing Default

I've the following: http://jsfiddle.net/KywJT/

function dragEnter(evt) {
    evt.stopPropagation();
    evt.preventDefault();
    $(evt.target).addClass('over');
}

function dragLeave(evt) {
    evt.stopPropagation();
    evt.preventDefault();
    $(evt.target).removeClass('over');
}

function drop(evt) {
   evt.stopPropagation();
   evt.preventDefault();
   $(evt.target).removeClass('over');
}

jQuery( function ( $ ) {

  var $box = $( "#box" );
  $box.bind("dragenter", dragEnter);
  $box.bind("dragleave", dragLeave);
  $box.bind("drop", drop);
});

I'm using Chrome version 24.0.1312.52 m and last jQuery (1.8.3). When I drop a file into the box, browser is not preventing default beaviour. Can you please help me?

P.S. dragexit is deprecated correct?

like image 598
user1824269 Avatar asked Jan 14 '13 20:01

user1824269


People also ask

How would you stop the default action of an event?

The preventDefault() method cancels the event if it is cancelable, meaning that the default action that belongs to the event will not occur. For example, this can be useful when: Clicking on a "Submit" button, prevent it from submitting a form. Clicking on a link, prevent the link from following the URL.

How is preventDefault () Used with drag and drop?

Most areas of a web page or application are not valid places to drop data. Thus, the default handling of these events is not to allow a drop. Calling the preventDefault() method during both a dragenter and dragover event will indicate that a drop is allowed at that location.

Why event preventDefault() is used?

The preventDefault() method is used to prevent the browser from executing the default action of the selected element. It can prevent the user from processing the request by clicking the link.

What is dragover in JavaScript?

The dragover event is fired when an element or text selection is being dragged over a valid drop target (every few hundred milliseconds). The event is fired on the drop target(s).


2 Answers

If you want the drop event to be fired in Google Chrome, a dragover listener has to be defined before, even if the function doesn't do anything at all. I don't know why, it's weird... but clearly identified and easy to reproduce :) Lost 2 hours on that sh*t yesterday :/ hope this helps

like image 109
Thibaut Pral Avatar answered Sep 18 '22 06:09

Thibaut Pral


This should fix the issue for you.

jQuery( function ( $ ) {
    var $box = $( "#box" );
    $box.bind("dragenter", dragEnter);
    $box.bind("dragleave", dragLeave);
    $box.bind("drop", drop);

    $(document).bind('dragover', function (e) {
         e.preventDefault();
    });
});
like image 37
Wolf Avatar answered Sep 18 '22 06:09

Wolf