On this page10 sections
Lecture 044: Drag and Drop API
| Category | 8: Web APIs |
| Lecture | 044 of 048 |
| Estimated video length | 12 min |
| Target | HTML5 · any modern browser |
| Level | Beginner |
Picking something up with the mouse and dropping it somewhere else feels natural: moving cards on a task board, sorting a list, uploading a file. HTML makes any element draggable, and a few events handle the drop.
Learning Objectives
- Make an element draggable with
draggable="true" - Handle
dragstart,dragoveranddrop - Pass data with
dataTransfer - Accept files dropped from the computer
1. The Three Steps
- Draggable item:
draggable="true", and ondragstartremember what is being dragged. - Drop zone: on
dragover, callevent.preventDefault(), because by default elements refuse drops. - Drop: on
drop, read the data and move the item.
2. A First Example
Example: drag the logo into the box:
<style>
#zone { width: 220px; height: 120px; border: 3px dashed #adb5bd; margin-top: 10px; display: grid; place-items: center; }
#zone.over { border-color: #2f9e44; background: #ebfbee; }
</style>
<img id="item" src="https://picsum.photos/id/237/100/70" alt="A puppy" draggable="true" width="100" height="70">
<div id="zone">Drop here</div>
<script>
const item = document.getElementById('item');
const zone = document.getElementById('zone');
item.addEventListener('dragstart', (e) => e.dataTransfer.setData('text/plain', e.target.id));
zone.addEventListener('dragover', (e) => { e.preventDefault(); zone.classList.add('over'); });
zone.addEventListener('dragleave', () => zone.classList.remove('over'));
zone.addEventListener('drop', (e) => {
e.preventDefault();
zone.classList.remove('over');
const id = e.dataTransfer.getData('text/plain');
zone.replaceChildren(document.getElementById(id));
});
</script>dataTransfer.setData stores a value at the start of the drag; getData reads it back on drop.
3. A Two-Column Task Board
Example: drag tasks between the columns:
<style>
.board { display: flex; gap: 12px; }
.col { flex: 1; min-height: 140px; padding: 8px; background: #f1f3f5; border-radius: 8px; }
.col h4 { margin: 0 0 8px; }
.task { padding: 8px; margin-bottom: 6px; background: white; border: 1px solid #dee2e6; border-radius: 6px; cursor: grab; }
</style>
<div class="board">
<div class="col"><h4>To do</h4>
<div class="task" draggable="true" id="t1">Buy groceries</div>
<div class="task" draggable="true" id="t2">Call Ammi</div>
<div class="task" draggable="true" id="t3">Finish homework</div>
</div>
<div class="col"><h4>Done</h4></div>
</div>
<script>
document.querySelectorAll('.task').forEach((t) =>
t.addEventListener('dragstart', (e) => e.dataTransfer.setData('text/plain', t.id)));
document.querySelectorAll('.col').forEach((col) => {
col.addEventListener('dragover', (e) => e.preventDefault());
col.addEventListener('drop', (e) => {
e.preventDefault();
col.append(document.getElementById(e.dataTransfer.getData('text/plain')));
});
});
</script>4. Dropping Files
Files dragged from the computer arrive in e.dataTransfer.files.
Example: drag a file from your desktop onto the box:
<div id="drop" style="padding: 30px; border: 3px dashed #74c0fc; text-align: center;">Drop a file here</div>
<ul id="files"></ul>
<script>
const drop = document.getElementById('drop');
drop.addEventListener('dragover', (e) => e.preventDefault());
drop.addEventListener('drop', (e) => {
e.preventDefault();
for (const file of e.dataTransfer.files) {
const li = document.createElement('li');
li.textContent = `${file.name} - ${(file.size / 1024).toFixed(1)} KB`;
document.getElementById('files').append(li);
}
});
</script>The Drag Events
| Event | Fires on | When |
|---|---|---|
dragstart | The item | The drag begins |
drag | The item | Continuously while dragging |
dragend | The item | The drag finishes |
dragenter | The drop zone | The item enters it |
dragover | The drop zone | The item moves over it (call preventDefault()) |
dragleave | The drop zone | The item leaves it |
drop | The drop zone | The item is dropped |
Common Beginner Mistakes
1. Forgetting preventDefault() in dragover. The drop never happens.
2. Forgetting draggable="true". Only images and links are draggable by default.
3. No keyboard alternative. Drag and drop doesn't work with a keyboard or on some touch screens. Offer buttons too ("Move to Done").
Practice Questions
- Which attribute makes an element draggable?
- Which event must call
preventDefault()for a drop to be allowed? - How do you pass the dragged element's id to the drop handler?
- Where do dropped files appear?
- Which event fires when the dragged item leaves a drop zone?
- Why should you offer an alternative to drag and drop?
Practice Answers
draggable="true"dragovere.dataTransfer.setData('text/plain', id)ondragstart, thengetData('text/plain')ondrop.e.dataTransfer.filesdragleave- Keyboard users and some touch-screen users can't drag.
Key Takeaways
draggable="true"+dragstarton the item;dragover(withpreventDefault) +dropon the zone.dataTransfercarries data from the drag to the drop.- Files from the computer arrive in
dataTransfer.files. - Always offer a non-drag way to do the same thing.