Warning: Attempt to read property "tagName" on null in /home/mrexamples/public_html/wp-content/themes/awaken/functions.php on line 1055
Warning: Attempt to read property "nodeValue" on null in /home/mrexamples/public_html/wp-content/themes/awaken/functions.php on line 1059
HTML Drag and Drop API
Today the topic of our discussion is HTML draganddrop .In this article, you will learn how to drag and drop any HTML element.
Example
Please place the mrexamples image inside the rectangle by dragging it.
Drag and Drop:
Using your mouse to drag and drop items is a frequent practice with HTML draganddrop. It’s “grabbing” an object and repositioning it somewhere.
Drag Events:
The drag-and-drop capability in HTML is based on DOM event models, with drag events originating from mouse events. The drag process generally involves selecting a draggable element, dragging it to a droppable element, and then dropping it when it comes to HTML draganddrop.
There are a variety of events that take place during drag operations, including dragover and drag events, which may happen multiple times.
There is a related event handler for every kind of drag event:
Event | Execute During |
---|---|
dragstart | When a user begins dragging an element or object. |
drag | A dragged item ( image or text selection ) is moved. |
dragleve | The item being dragged leaves a valid drop target. |
dragend | When you release a mouse button or hit Esc, a drag operation ends. |
dragover | About every 0.1 seconds, an item is being dragged over a valid drop target. |
drop | When an object is dropped on a valid drop target. |
dragenter | Draggable items enter valid drop targets. |
Browser Compatibility:
The table’s numbers denote the first browser version to include complete Drag and Drop compatibility as it comes to HTML draganddrop.
API | |||||
---|---|---|---|---|---|
Drag and Drop | 4.0 | 9.0 | 3.5 | 6.0 | 12.0 |
HTML Drag and Drop Example:
A basic drag-and-drop scenario is shown below:
Example
Create a Draggable Element:
To begin with, in HTML draganddrop, the draggable attribute must be valid for an element to be dragged and dropped.
ondragstart and setData() – What to Drag?
Then, declare the expected behavior of the dragged element.
The ondragstart attribute in the above code sample calls a drag(event) method, passing in the event and the data to be dragged.
You can change the type and value of the dragged data with the dataTransfer.setData() method as it comes to HTML draganddrop.
function drag(ev) { ev.dataTransfer.setData(“text”, ev.target.id); }
In HTML draganddrop, the id of the dragged element (“drag1”) is the value & the data type is “text.”
Ondragover – Where to Drop?
Data can be dropped at a predetermined location by triggering the ondragover event.
It is impossible to drop data or elements into other elements by default. To permit a drop, we must disable the element’s standard processing.
To do this, we use the ondragover event’s event.preventDefault() method in HTML draganddrop:
event.preventDefault()
Do the Drop – ondrop
A drop event occurs when the data that was being dragged is released.
The drop(event) method is invoked in the preceding example via the ondrop attribute when it comes to HTML draganddrop :
var data = ev.dataTransfer.getData(“text”);
ev.target.appendChild(document.getElementById(data));
}
Detailed Explanation of the Code:
- To stop the browser from processing the data in its default manner, you can use the preventDefault() function (default is open as the link on the drop).
- The dataTransfer.getData() method can retrieve the dragged information. Any information of the same kind that is sent to setData() will be returned.
- The element id is what was dragged. Thus that’s what you’ll find in the data (“drag1”).
- Combining the element you dragged into the one you dropped.
Example: 
HTML Dragging Text
In this Drag and drop example, we demonstrate dragging text in a rectangular box:
Example: 
HTML Draggable
The example below illustrates the draggable property usage there are three boxes in the example and all of them are draggable:
Example: 
Use of HTML drag and drop
HTML drag and drop is a feature that allows users to drag an object and drop it to a different location on a web page. This feature has several useful applications:
- File Upload: Drag and drop can be used to upload files to a website. Users can simply drag a file from their computer and drop it onto the website, without having to navigate through folders or click on a button to select a file.
- Sorting and Reordering: Drag and drop can be used to sort and reorder elements on a web page. For example, users can drag and drop items in a shopping cart to change their order, or drag and drop items in a to-do list to prioritize them.
- Interactive Games: Drag and drop can be used to create interactive games and puzzles on a web page. Users can drag and drop objects to solve puzzles, build structures, or complete challenges.
- Workflow Automation: Drag and drop can be used to automate workflows and simplify complex tasks. Users can drag and drop files, folders, or other elements to trigger automated processes, such as file conversions or data analysis.