TExcelTAD
Home › HTML › HTML Drag and Drop

HTML Drag and Drop

Dragging things around — files into an upload area, cards between columns, items into a basket — feels natural to users. HTML has a built-in Drag and Drop API that makes any element draggable and lets other elements accept drops.

The three ingredients

  1. Make an element draggable with the draggable="true" attribute. (Images and links are draggable by default.)
  2. When dragging starts, store some data about what is being dragged (dragstart event).
  3. On the target, allow dropping (dragover + preventDefault()) and handle the result (drop event).

A first example

Drag the star into the box.

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  #star { font-size: 40px; cursor: grab; display: inline-block; }
  #box { width: 200px; height: 100px; border: 3px dashed #999; margin-top: 12px;
         display: flex; align-items: center; justify-content: center; }
</style>
</head>
<body>
  <span id="star" draggable="true">⭐</span>
  <div id="box">Drop here</div>
  <script>
    const star = document.getElementById('star');
    const box = document.getElementById('box');

    star.addEventListener('dragstart', function (e) {
      e.dataTransfer.setData('text/plain', e.target.id);
    });
    box.addEventListener('dragover', function (e) {
      e.preventDefault(); // without this, dropping is not allowed
    });
    box.addEventListener('drop', function (e) {
      e.preventDefault();
      const id = e.dataTransfer.getData('text/plain');
      box.textContent = '';
      box.appendChild(document.getElementById(id));
    });
  </script>
</body>
</html>

The key line people forget is e.preventDefault() in dragover. By default, elements refuse drops; cancelling the default behaviour says "yes, you may drop here".

The drag events

Event Fires on When
dragstart Dragged element The user starts dragging
drag Dragged element Repeatedly while dragging
dragend Dragged element Dragging finishes (dropped or cancelled)
dragenter Drop target The dragged item enters the target
dragover Drop target Repeatedly while over the target
dragleave Drop target The item leaves the target
drop Drop target The item is released over the target

The dataTransfer object

Every drag event has an event.dataTransfer object that carries information from the source to the target.

Member Purpose
setData(type, value) Store data, e.g. 'text/plain'
getData(type) Read it back in drop
effectAllowed / dropEffect 'move', 'copy' or 'link' — changes the cursor
files Files dragged in from the user's computer

Visual feedback and multiple targets

Users need to see where they can drop. Use dragenter/dragleave to highlight the target. This mini task board moves cards between two columns.

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: sans-serif; }
  .col { display: inline-block; vertical-align: top; width: 140px; min-height: 160px;
         padding: 8px; margin: 4px; background: #f1f3f4; border-radius: 8px; }
  .col.over { background: #d2e3fc; }
  .card { background: white; padding: 8px; margin: 6px 0; border-radius: 6px;
          box-shadow: 0 1px 3px rgba(0,0,0,.2); cursor: grab; }
</style>
</head>
<body>
  <div class="col" id="todo"><strong>To do</strong>
    <div class="card" draggable="true" id="c1">Learn tables</div>
    <div class="card" draggable="true" id="c2">Learn forms</div>
  </div>
  <div class="col" id="done"><strong>Done</strong>
    <div class="card" draggable="true" id="c3">Learn links</div>
  </div>
  <script>
    document.querySelectorAll('.card').forEach(function (card) {
      card.addEventListener('dragstart', function (e) {
        e.dataTransfer.setData('text/plain', card.id);
        e.dataTransfer.effectAllowed = 'move';
      });
    });
    document.querySelectorAll('.col').forEach(function (col) {
      col.addEventListener('dragenter', function () { col.classList.add('over'); });
      col.addEventListener('dragleave', function (e) {
        if (!col.contains(e.relatedTarget)) col.classList.remove('over');
      });
      col.addEventListener('dragover', function (e) { e.preventDefault(); });
      col.addEventListener('drop', function (e) {
        e.preventDefault();
        col.classList.remove('over');
        col.appendChild(document.getElementById(e.dataTransfer.getData('text/plain')));
      });
    });
  </script>
</body>
</html>

Dropping files

When the user drags files from their desktop, they arrive in e.dataTransfer.files. You can read their names, sizes and types — the file never leaves the user's computer unless your code uploads it.

html · try it yourself
<!DOCTYPE html>
<html>
<body>
  <div id="zone" style="border:3px dashed #888;padding:30px;text-align:center">
    Drag a file from your computer onto this box
  </div>
  <ul id="files"></ul>
  <script>
    const zone = document.getElementById('zone');
    zone.addEventListener('dragover', function (e) { e.preventDefault(); });
    zone.addEventListener('drop', function (e) {
      e.preventDefault();
      const list = document.getElementById('files');
      list.innerHTML = '';
      for (const f of e.dataTransfer.files) {
        const li = document.createElement('li');
        li.textContent = f.name + ' — ' + f.size + ' bytes (' + (f.type || 'unknown type') + ')';
        list.appendChild(li);
      }
    });
  </script>
</body>
</html>

Note: Native drag and drop does not work with touch on many mobile browsers, and it is impossible for keyboard-only users. Always offer an alternative — for example "Move to Done" buttons, or a normal <input type="file"> next to a drop zone.

Test yourself

Which attribute makes a <div> draggable?

  1. drag="on"
  2. draggable="true"
  3. movable
  4. dropzone

Why must you call preventDefault() in the dragover handler?

  1. To stop the page scrolling
  2. Because elements reject drops by default; cancelling the default allows dropping
  3. To make the drag faster
  4. It is optional and has no effect

Where do files dragged from the desktop appear?

  1. event.target.files
  2. event.dataTransfer.files
  3. window.files
  4. document.drop