1

I have drag and drop code that works normally in Chrome and Firefox but it won't work in Internet Explorer 11 and Edge.

This is simplified example of moving nodes in treeview. You drag one node (li element) and put it on another (also li element). So when you drop element to another, it shows alert what is moved on what. And that works in Chrome anf FF but don't work in IE and Edge. Anyone knows why? It seems that IE and Edge handles Drag and Drop differently than other browsers, but I can not figure out what to change.

Here is code (whole page):

<!DOCTYPE html>
<html>

    <head>
        <title>DD Test</title>

        <!-- jQuery -->
        <link rel="stylesheet" href="https://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
        <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
    </head>

    <body>
        <ul>
            <li draggable="true" ondragstart="drag(event)" ondrop="drop(event)" ondragover="allowDrop(event)">Coffee</li>
            <li draggable="true" ondragstart="drag(event)" ondrop="drop(event)" ondragover="allowDrop(event)">Tea</li>
            <li draggable="true" ondragstart="drag(event)" ondrop="drop(event)" ondragover="allowDrop(event)">Milk</li>
        </ul>     
    </body>

</html>


<script>
    function allowDrop(ev) {
        ev.preventDefault();
    }

    function drag(ev) {
        ev.stopPropagation();
        var object = $(ev.target);
        var objectname = object.html();
        ev.dataTransfer.setData("objectname", objectname);
    }

    function drop(ev) {
        ev.preventDefault();
        ev.stopPropagation();
        var target = $(ev.target);
        var targetname = target.html();
        var objectname = ev.dataTransfer.getData("objectname");
        alert(objectname + " on " + targetname);
    }
</script>

function allowDrop(ev) {
  ev.preventDefault();
}

function drag(ev) {
  ev.stopPropagation();
  var object = $(ev.target);
  var objectname = object.html();
  ev.dataTransfer.setData("objectname", objectname);
}

function drop(ev) {
  ev.preventDefault();
  ev.stopPropagation();
  var target = $(ev.target);
  var targetname = target.html();
  var objectname = ev.dataTransfer.getData("objectname");
  alert(objectname + " on " + targetname);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<ul>
  <li draggable="true" ondragstart="drag(event)" ondrop="drop(event)" ondragover="allowDrop(event)">Coffee</li>
  <li draggable="true" ondragstart="drag(event)" ondrop="drop(event)" ondragover="allowDrop(event)">Tea</li>
  <li draggable="true" ondragstart="drag(event)" ondrop="drop(event)" ondragover="allowDrop(event)">Milk</li>
</ul>
SaleCar
  • 1,086
  • 9
  • 23
  • could you explain what you are doing with this code? – Random Channel May 22 '16 at 14:13
  • do you want it so you can reorganize the list? – Random Channel May 22 '16 at 14:19
  • This is simplified example of moving nodes in treeview. You drag one node (
  • element) and put it on another (also
  • element). Of course, real project is much complex than this and this is just to show how drag and drop works on Chrome and FF but dont work on IE and Edge. Just imagine that instead of "Alert" there is function that works something usefull.
  • – SaleCar May 22 '16 at 14:22
  • Perhaps you need to get the event from window.event: http://stackoverflow.com/questions/1000597/event-preventdefault-function-not-working-in-ie – mplungjan May 22 '16 at 16:21
  • Nope. I changed code as suggested on that topic but still dont work. – SaleCar May 22 '16 at 16:29