The HTML Drag & Drop API lets users drag elements (or data) and drop them somewhere else on the page. It’s widely used for reordering lists, file uploads, building kanban boards, and interactive UIs.
Below is a compact, practical guide you can use on your Learn2Kode tutorial page: explanation, events, attributes, accessibility tips, and a working example.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Drag & Drop Example</title>
<style>
.container { display:flex; gap:20px; padding:20px; }
.box {
width:220px; min-height:150px;
border:2px dashed #99a; padding:10px; border-radius:6px;
background:#f8faff;
}
.item {
padding:10px 12px; margin:8px 0;
background:#fff; border:1px solid #ddd; border-radius:4px;
cursor:grab;
}
.item:active { cursor:grabbing; }
.over { background:#e6f7ff; border-color:#4aa3ff; }
</style>
</head>
<body>
<h1>Drag & Drop Demo</h1>
<div class="container">
<!-- Draggable source -->
<div class="box" id="source">
<h3>Available Items</h3>
<div class="item" draggable="true" id="item1">Item 1</div>
<div class="item" draggable="true" id="item2">Item 2</div>
<div class="item" draggable="true" id="item3">Item 3</div>
</div>
<!-- Drop target -->
<div class="box" id="target">
<h3>Drop Here</h3>
<p style="color:#666;font-size:14px">Drop items from the left box into this box.</p>
</div>
</div>
<script>
// set up dragstart on items
document.querySelectorAll('.item').forEach(item => {
item.addEventListener('dragstart', (e) => {
// set id as transfer data
e.dataTransfer.setData('text/plain', e.target.id);
// optional: set drag image or effect
e.dataTransfer.effectAllowed = 'move';
e.target.style.opacity = '0.6';
});
item.addEventListener('dragend', (e) => {
e.target.style.opacity = ''; // cleanup
});
});
const target = document.getElementById('target');
// necessary to allow dropping
target.addEventListener('dragover', (e) => {
e.preventDefault(); // allow drop
e.dataTransfer.dropEffect = 'move';
target.classList.add('over');
});
target.addEventListener('dragleave', () => {
target.classList.remove('over');
});
target.addEventListener('drop', (e) => {
e.preventDefault(); // prevent browser default handling
target.classList.remove('over');
// read transferred data (the id of the dragged element)
const id = e.dataTransfer.getData('text/plain');
const dragged = document.getElementById(id);
// append dragged node into drop target (moves the element)
if (dragged) {
target.appendChild(dragged);
}
});
</script>
</body>
</html>
Drop items from the left box into this box.
Drag & Drop API is mouse/touch-centric — for accessibility you must provide keyboard equivalents:
Native Drag & Drop support on mobile browsers is inconsistent. For touch devices, implement touch event fallbacks (touchstart, touchmove, touchend) or use libraries that unify drag across devices (e.g., SortableJS, interact.js).
If you want sortable lists, multi-touch support, or advanced features with minimal code, consider a lightweight library:
<table border="1" cellpadding="6">
<tr><th>Event</th><th>When</th></tr>
<tr><td>dragstart</td><td>Drag begins on draggable element</td></tr>
<tr><td>drag</td><td>Element is being dragged</td></tr>
<tr><td>dragenter</td><td>Dragged item enters a drop target</td></tr>
<tr><td>dragover</td><td>Dragged item is over a drop target (call preventDefault())</td></tr>
<tr><td>dragleave</td><td>Dragged item leaves a drop target</td></tr>
<tr><td>drop</td><td>Item is dropped on a target</td></tr>
<tr><td>dragend</td><td>Drag operation finished</td></tr>
</table>
| Event | When |
|---|---|
| dragstart | Drag begins on draggable element |
| drag | Element is being dragged |
| dragenter | Dragged item enters a drop target |
| dragover | Dragged item is over a drop target (call preventDefault()) |
| dragleave | Dragged item leaves a drop target |
| drop | Item is dropped on a target |
| dragend | Drag operation finished |