HTML5 Drag and Drop

HTML5 Tutorial

Drag and Drop

Completed

Drag and drop is known to be a very common feature. It happens when you are "grabbing" an object and dragging it to a someother location.

In the latest HTML5, drag and drop is included, and any element is draggable.

Browser Support

Completed

The table numbers specifies the first browser version which fully supported Drag and Drop.

API
Drag and Drop 4.0 9.0 3.5 6.0 12.0

HTML Drag and Drop Example

Completed

The example given below illustrates a simple drag and drop scenario:

Example

<!DOCTYPE HTML>
<html>
<head>
<script>
function allowDrop(ev) {
    ev.preventDefault();
}

function drag(ev) {
    ev.dataTransfer.setData("text", ev.target.id);
}

function drop(ev) {
    ev.preventDefault();
    var data = ev.dataTransfer.getData("text");
    ev.target.appendChild(document.getElementById(data));
}
</script>
</head>
<body>

<div id="div1" ondrop="drop(event)" ondragover="allowDrop(event)"></div>

<img id="drag1" src="img_logo.gif" draggable="true"
ondragstart="drag(event)" width="336" height="69"
>


</body>
</html>
Try it Yourself

It may look complicated, but after going through all the parts of a drag and drop event we can understand it clearly.

Make an Element Draggable

Completed

First: For making an element draggable, you need to change the draggable attribute to true:

Example

<img draggable="true">

What to Drag - ondragstart and setData()

Completed

Later,you need to specify what should be happening if the element is dragged.

In the example given above, the ondragstart attribute is calling a function, drag(event), which is specifying what data should be dragged.

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

function drag(ev) {
    ev.dataTransfer.setData("text", ev.target.id);
}

Here, the data type is given as "text" and the value given is the draggable element id ("drag1").

Where to Drop - ondragover

Completed

The ondragover event is used to specify where the dragged data should be dropped.

With default settings, data/elements is not possible to be dropped in some other elements. For allowing a drop, we must be preventing the default handling of the element.

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

event.preventDefault()

Do the Drop - ondrop

Completed

When the dragged data is getting dropped, a drop event is occuring.

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

function drop(ev) {
    ev.preventDefault();
    var data = ev.dataTransfer.getData("text");
    ev.target.appendChild(document.getElementById(data));
}

Code explained:

  • Call preventDefault() is used to prevent the browser from default handling of the data (default setting is open as link on drop)
  • Get the dragged data using the dataTransfer.getData() method. This method will be returning any data that was being set to the same type in the setData() method
  • The dragged data consists of the id of the dragged element ("drag1")
  • Appending of the dragged element to be done into the drop element