Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The drag event not firing in Firefox or IE

In Chrome the drag event is fired and logged to the console. In Firefox and IE, it does not.

<html>
<head>

<style>
  #d {
    width:20px;
    height:20px;
    background-color: red;
  }
</style>

</head>
<body>

<div id="d" draggable="true"></div>

<script>
    d = document.getElementById('d');
    d.addEventListener('dragstart', function(e){
      console.log("dragstart:", e);
    });
    d.addEventListener('drag', function(e){
      console.log("drag:", e);
    });
</script>

</body>
</html>

Fiddle version: http://jsfiddle.net/korimako/e1wqafyr

How do I set up a div to dispatch drag events and listen for them properly?

like image 523
Korimako Avatar asked Jan 05 '15 22:01

Korimako


1 Answers

Firefox requires that dataTransfer is set before the drag event is fired

d = document.getElementById('d');

d.addEventListener('drag', function(e){
    console.log("drag:", e)
});

d.addEventListener('dragstart', function(e){
    e.dataTransfer.setData('application/node type', this);
    console.log("dragstart:", e)
});

FIDDLE

See this for drag types

like image 95
adeneo Avatar answered Nov 12 '22 06:11

adeneo