1. Fundamentals of HTML5 Drag & Drop (draggable="true")
The HTML5 Drag & Drop API enables native dragging of DOM elements across drop targets without relying on heavy external JavaScript libraries.
- Default Draggable Elements: Text selections,
<img>elements, and<a href="...">hyperlinks are draggable by default. - Custom Draggable Elements: Any HTML element (e.g.
<div>,<li>,<article>) becomes draggable by adding the global attributedraggable="true".
draggable attribute is NOT a standard boolean attribute. You must explicitly specify draggable="true" or draggable="false" (omitting the value does not default to true).
Making a Custom Box Draggable
Toggle the draggable attribute below to test dragging behavior on/off:
2. Drag & Drop Event Lifecycle & Flow
The Drag & Drop lifecycle split into two distinct event streams: events fired on the Drag Source element, and events fired on the Drop Target zone.
Drag and Drop Event Lifecycle & Transfer Data Flow
Handling Source and Target Events
Drag the package into the target zone to inspect event triggers live:
3. The dataTransfer Object & Payload Storage
The event.dataTransfer object holds the payload passed from the drag source to the drop target during the drag operation.
| Method / Property | Description |
|---|---|
setData(format, data) |
Stores data payload. Standard formats include 'text/plain', 'text/html', 'text/uri-list', or JSON strings. |
getData(format) |
Retrieves stored payload string inside the drop event listener. |
clearData([format]) |
Clears all data or specified format data from payload storage. |
effectAllowed |
Specifies cursor feedback allowed by source: 'copy', 'move', 'link', 'copyMove', 'all', 'none'. |
dropEffect |
Specifies actual drop effect chosen by target: 'copy', 'move', 'link', 'none'. |
Passing Complex JSON Payloads
Drag custom payload cards below into the inspector to extract data:
4. Drop Target Visual Feedback & Cursor Modification
Providing clear visual feedback when an element hovers over a valid drop target is crucial for user experience.
Toggling Active Classes on Drag Enter / Leave
Drag the element into different drop effect zones to observe cursor modifications:
5. Interactive Project: Drag-and-Drop Task Board
Putting it all together: build a production-grade Kanban Task Board where tasks can be dragged between status columns ("To Do", "In Progress", "Done").
Kanban Board Drag & Drop Architecture
Drag cards between status columns to manage tasks live:
Hands-On Challenge: Build a File Drop Zone Uploader
Create a file upload drop zone using event.dataTransfer.files that highlights green on dragenter and displays the dropped file's name and file size in KB.
Chapter 17 Key Takeaways
- Add
draggable="true"to make custom HTML elements draggable. - Must call
event.preventDefault()insideondragoverfor target elements to accept drops. - Use
event.dataTransfer.setData(format, data)ondragstartto attach payloads. - Use
event.dataTransfer.getData(format)ondropto extract payload data. - Use
effectAllowedanddropEffectto control cursor icons (`copy`, `move`, `link`).