Drag and Drop API
The HTML Drag and Drop API enables an element to be dragged and dropped.
The HTML Drag and Drop API enables an element to be dragged and dropped.
Drag and drop is a very common feature. It is when you "grab" an object and drag it to a different location.
Drag the EA Academy image into the second rectangle.
Browser Support
The numbers in the table specify the first browser version that fully supports Drag and Drop API.
| API | |||||
|---|---|---|---|---|---|
| Drag and Drop | 4 | 9 | 3.5 | 6 | 12 |
Drag and Drop API Example
The example below is a simple drag and drop example:
Example
Example Code
htmlIt 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:
Example Code
htmlor:
Example Code
htmlWhat to Drag - ondragstart and setData()
Then, specify what should happen when the element is dragged.
In the example above, the ondragstart attribute
of the <img> element calls a function (dragstartHandler(ev)),
that specifies what data to be dragged.
The dataTransfer.setData() method sets the data type and the value of the
dragged data:
Example Code
htmlfunction dragstartHandler(ev) {
ev.dataTransfer.setData("text",
ev.target.id);
}In this case, the data type is "text" and the value is the id of the draggable element ("img1").
Where to Drop - ondragover
The ondragover attrribute of the <div>
element calls a function (dragoverHandler(ev)), that 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 preventDefault() method for the ondragover event:
Example Code
htmlfunction dragoverHandler(ev) {
ev.preventDefault();
}Do the Drop - ondrop
When the dragged data is dropped, a drop event occurs.
In the example above, the ondrop attribute of the <div> element calls a function, dropHandler(event):
Example Code
htmlfunction dropHandler(ev) {
ev.preventDefault();
const
data = ev.dataTransfer.getData("text");
ev.target.appendChild(document.getElementById(data));
}Code explained:
- Call
preventDefault()to prevent the browser default handling of the data (default is open as link on drop)
- Get the dragged data with the
dataTransfer.getData()method. This method will return any data that was set to the same type in thesetData()method
- The dragged data is the id of the dragged element ("img1")
- Append the dragged element into the drop element
More Examples
Example
Example Code
htmlExample
Example Code
htmlExample
How to drag and drop an image back and forth between two <div> elements: