On this page10 sections
  1. Learning Objectives
  2. 1. The Three Steps
  3. 2. A First Example
  4. 3. A Two-Column Task Board
  5. 4. Dropping Files
  6. The Drag Events
  7. Common Beginner Mistakes
  8. Practice Questions
  9. Practice Answers
  10. Key Takeaways

Lecture 044: Drag and Drop API

Category8: Web APIs
Lecture044 of 048
Estimated video length12 min
TargetHTML5 · any modern browser
LevelBeginner

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, dragover and drop
  • Pass data with dataTransfer
  • Accept files dropped from the computer

1. The Three Steps

  1. Draggable item: draggable="true", and on dragstart remember what is being dragged.
  2. Drop zone: on dragover, call event.preventDefault(), because by default elements refuse drops.
  3. Drop: on drop, read the data and move the item.

2. A First Example

Example: drag the logo into the box:

html
<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:

html
<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:

html
<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

EventFires onWhen
dragstartThe itemThe drag begins
dragThe itemContinuously while dragging
dragendThe itemThe drag finishes
dragenterThe drop zoneThe item enters it
dragoverThe drop zoneThe item moves over it (call preventDefault())
dragleaveThe drop zoneThe item leaves it
dropThe drop zoneThe 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

  1. Which attribute makes an element draggable?
  2. Which event must call preventDefault() for a drop to be allowed?
  3. How do you pass the dragged element's id to the drop handler?
  4. Where do dropped files appear?
  5. Which event fires when the dragged item leaves a drop zone?
  6. Why should you offer an alternative to drag and drop?

Practice Answers

  1. draggable="true"
  2. dragover
  3. e.dataTransfer.setData('text/plain', id) on dragstart, then getData('text/plain') on drop.
  4. e.dataTransfer.files
  5. dragleave
  6. Keyboard users and some touch-screen users can't drag.

Key Takeaways

  • draggable="true" + dragstart on the item; dragover (with preventDefault) + drop on the zone.
  • dataTransfer carries 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.