← Back to HTML Course | Chapter 7: Advanced APIs & Features | Lesson 4 of 11

HTML Drag and Drop

Imagine working at an office desk and sorting through folders. If you want to put a document inside a cabinet, you do not type out a command or select options on a screen. You simply pick up the folder and slide it directly into the cabinet drawer. HTML Drag and Drop brings this natural interaction directly onto your webpage, allowing users to grab elements, slide them across the screen, and drop them into target zones. It is the perfect tool for building interactive task boards, shopping carts, or drag-and-drop file uploaders.

Understanding Drag and Drop

HTML Drag and Drop uses standard browser events to manage interactions. Elements can be made draggable, drop targets are configured to accept drops, and data is transferred between them securely.

Note: Use drag-and-drop interactions to make complex tasks like file uploading intuitive for your users.

Warning: Drag-and-drop interactions can be difficult to use on touchscreen mobile devices without custom helper scripts.

Example: Understanding Drag and Drop

markup
<div draggable="true" ondragstart="event.dataTransfer.setData('text', 'item1')">Drag me</div>
<div ondrop="event.preventDefault()" ondragover="event.preventDefault()">Drop here</div>

Making Elements Draggable

To make any element draggable, you add the draggable="true" attribute to its opening tag and bind an ondragstart event listener to specify what data should be transferred. Without this attribute the element remains selectable text or a static image, and dragging it does nothing.

Note: Always specify a unique ID on your draggable elements so your scripts can identify them during the drag-and-drop flow.

Warning: Images and links are draggable by default; you must set draggable="false" if you want to lock them in place.

Example: Making Elements Draggable

markup
<div id="item1" draggable="true" ondragstart="event.dataTransfer.setData('text', 'item1')">
  Drag me
</div>

Configuring Drop Targets

By default, browsers prevent dropping elements on webpage layouts. To turn an element into a drop target, you must bind an ondragover event and call preventDefault to allow dropping.

Note: Use simple visual hover styles to show users when they are hovering over a valid drop target.

Warning: If you forget to call preventDefault on the dragover event, the browser will block all drops.

Example: Configuring Drop Targets

markup
<div ondragover="event.preventDefault()" ondrop="event.preventDefault()">
  Drop Zone
</div>

Transferring Data with DataTransfer

The DataTransfer object manages the data exchange during the drag-and-drop process. It allows you to save values (like text, IDs, or URLs) on drag start and retrieve them on drop. Common formats used include 'text/plain' for simple text and 'text/uri-list' for links or file references.

Note: Use DataTransfer to pass clean, structured JSON strings between your drag-and-drop elements.

Warning: DataTransfer is only accessible during dragstart and drop events; trying to read it during dragover will return empty values.

Example: Transferring Data with DataTransfer

markup
<div draggable="true" ondragstart="event.dataTransfer.setData('text/plain', 'Hello')">Drag</div>
<div ondragover="event.preventDefault()" ondrop="console.log(event.dataTransfer.getData('text/plain'))">
  Drop
</div>

Dragging Local Files into Drop Zones

The most common use case for drag-and-drop is letting users drag physical files directly from their computers and drop them into your web application to upload them. Reading these files requires the FileReader API once they land in the drop zone's event handler.

Note: Use standard file reader scripts to process and display image previews as soon as users drop their files.

Warning: Make sure your drop zones have clear borders so users know where to drop their files.

Example: Dragging Local Files into Drop Zones

markup
<div ondragover="event.preventDefault()" ondrop="console.log(event.dataTransfer.files)">
  Drop files here
</div>
Common Mistakes
  1. Forgetting to call preventDefault on the dragover event, which blocks all drops.
  2. Attempting to read DataTransfer values during the dragover event.
  3. Omitting support for touchscreen devices, leaving mobile users unable to interact with your site.
Chapter Summary
  • Enable drag-and-drop interactions on your webpage by adding draggable="true" to your tags.
  • Use dragover and drop events to turn containers into active drop targets.
  • Transfer custom data and files securely between elements using the DataTransfer object.
Browser Support

Standard HTML5 Drag and Drop events are natively supported by all modern desktop browsers.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.