HTML Drag and Drop interfaces enable applications to use drag-and-drop features in browsers.
The user may select draggable elements with a mouse, drag those elements to a droppable element, and drop them by releasing the mouse button. A translucent representation of the draggable elements follows the pointer during the drag operation.
For web sites, extensions, and XUL applications, you can customize which elements can become draggable, the type of feedback the draggable elements produce, and the droppable elements.
This overview of HTML Drag and Drop includes a description of the interfaces, basic steps to add drag-and-drop support to an application, and an interoperability summary of the interfaces.
Browser Support
The numbers in the table specify the first browser version that fully supports Drag and Drop.
HTML Drag and Drop 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>
It might seem complicated, but let’s 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) {
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 (“drag1”).
Where to Drop – ondragover
The ondragover
the 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:event.preventDefault()
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) {
ev.preventDefault();
var 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 the setData() method
- The dragged data is the id of the dragged element (“drag1”)
- Append the dragged element into the drop element
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) {
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 (“drag1”).
Where to Drop – ondragover
The ondragover
the 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:event.preventDefault()
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) {
ev.preventDefault();
var 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 the setData() method
- The dragged data is the id of the dragged element (“drag1”)
- Append the dragged element into the drop element