HTML5 Drag and Drop Demo

I’ve done some research about HTML5 APIs, today I want to show you the Drag and Drop.

It’s pretty cool and best of all works in all major browsers!

So I’ve built a simple demo playing with containers and HTML data attributes. We’ll retrieve the containers attributes once we drop them in the drop zone.

Costs App Calculator

Let’s get into it.

The HTML

Fairly simple HTML, all you need is the drop zone and the draggable items:

The JavaScript

Here comes the juicy part. Basically what we want to do is add event listeners to the draggable items and to the drop zone.

We’ll use callback functions to create actions when the items are dropped inside the drop zone.

I’m using jQuery just as a selector and for DOM manipulation. You could use pure JavaScript or an other library of your choice, it’s not required for the code to work.

What I’ve done with the demo is retrieve the data attributes and print them inside the drop zone (check the Demo page source code for full details)

What about mobile?

HTML5 got you covered! There are touch events ready to boost your code on mobile devices.

Check out the full list on HTML5 Rocks.

Unfortunately I haven’t been able to completely adapt this demo for touch devices, I’ll update as soon as I can.

View Demo

7 Responses to “HTML5 Drag and Drop Demo”

  1. JB April 27, 2014 at 8:09 pm #

    Great tutorial! I did notice that the drag and drop functionality does not work in FireFox. Any suggestions how to make drag and drop work in FF?

    • Rick April 28, 2014 at 8:24 am #

      Hi JB,

      Thanks for the heads up! I’ve fixed the demo to work on Firefox.

      Enjoy.

      • jb April 29, 2014 at 12:55 am #

        Hi Rick,

        I was able to fix the problem as well by adding

        ev.dataTransfer.setData(‘Text’, this.id); // required otherwise doesn’t work

        I see you did the same. Any idea why FireFox requires this line of code in order to work?

      • Rick April 29, 2014 at 11:56 am #

        I don’t. Check out the Mozilla Developer documentation, it might give you some explanation: https://developer.mozilla.org/en-US/docs/DragDrop/Recommended_Drag_Types#text

  2. Danno June 12, 2016 at 4:22 pm #

    In 2016, othing happening in IE11 with drag & drop.

  3. James August 3, 2017 at 7:31 pm #

    Not working in Chrome currently

Leave a Reply

Add <code> Some Code </code> by using this tags.