Approach for Drag and Drop in HTML

  • Set an element as draggable with the draggable attribute: <img draggable="true">.
  • Specify the drag behavior using the ondragstart attribute, calling a function (drag(event)) to define the data to be dragged with event.dataTransfer.setData().
  • Utilize the ondragover event to determine where the data can be dropped. Prevent default behavior with event.preventDefault() to enable the drop.
  • Implement the ondrop event to perform the actual drop action.

HTML Drag and Drop

Drag and Drop is a very interactive and user-friendly concept that makes it easier to move an object to a different location by grabbing it. This allows the user to click and hold the mouse button over an element, drag it to another location, and release the mouse button to drop the element there.

Table of Content

  • Drag and Drop Events
  • The Data Transfer Object
  • Approach for Drag and Drop in HTML
  • Examples Showing Drag and Drop in HTML

In HTML5 Drag and Drop are much easier to code and any element in it is draggable.

Similar Reads

Drag and Drop Events

...

The Data Transfer Object

The data transfer property is used when the whole process of Drag and Drop happens. It is used to hold the dragged data from the source and drop it to the desired location. These are the properties associated with it:...

Approach for Drag and Drop in HTML

Set an element as draggable with the draggable attribute: . Specify the drag behavior using the ondragstart attribute, calling a function (drag(event)) to define the data to be dragged with event.dataTransfer.setData(). Utilize the ondragover event to determine where the data can be dropped. Prevent default behavior with event.preventDefault() to enable the drop. Implement the ondrop event to perform the actual drop action....

Examples Showing Drag and Drop in HTML

1. Drag and Drop of Image in HTML:...