MEMEPh. ideas that are worth sharing...

HTML Drag and Drop API

In HTML, any element can be dragged and dropped.





Drag the MEMEPh image into the rectangle.

Drag and Drop

Drag and drop is a very common feature. It is when you "grab" an object and drag it to a different location.

Browser Support

The numbers in the table specify the first browser version that fully supports Drag and Drop.

<Drag and Drop> 4.0 9.0 3.5 6.0 12.0

HTML Drag and Drop Example

The example below is a simple drag and drop example:


#div1 {
  width: 350px;
  height: 70px;
  padding: 10px;
  border: 1px solid #aaaaaa;
function allowDrop(ev) {

function drag(ev) {

function drop(ev) {
  var data = ev.dataTransfer.getData("text");;

<p>Drag the MEMEPh image into the rectangle:</p>

<div id="div1" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
<img id="drag1" src="/cdn/logo/icon.jpg" draggable="true" ondragstart="drag(event)" width="106" height="69">


Try it Yourself »

It might seem complicated, but lets go through all the different parts of a drag and drop event.

Make an Element Draggable

First of all: To make an element draggable, set the draggable attribute to true:

<img draggable="true">

What to Drag - ondragstart and setData()

Then, specify what should happen when the element is dragged.

In the example above, the ondragstart attribute calls a function, drag(event), that specifies what data to be dragged.

The dataTransfer.setData() method sets the data type and the value of the dragged data:

function drag(ev) {

In this case, the data type is "text" and the value is the id of the draggable element ("drag1").

Where to Drop - ondragover

The ondragover event specifies where the dragged data can be dropped.

By default, data/elements cannot be dropped in other elements. To allow a drop, we must prevent the default handling of the element.

This is done by calling the event.preventDefault() method for the ondragover event:


Do the Drop - ondrop

When the dragged data is dropped, a drop event occurs.

In the example above, the ondrop attribute calls a function, drop(event):

function drop(ev) {
  var data = ev.dataTransfer.getData("text");;

Code explained:

More Examples


How to drag (and drop) an image back and forth between two <div> elements:

Drag and Drop

Drag the image back and forth between the two div elements.


Try it Yourself »