0Pricing
JavaScript Academy · Lesson

Drop Targets and dataTransfer

Accept drops and transfer data.

What Makes a Drop Target?

By default elements reject drops. To accept a drop you must prevent the default behavior on both dragover and drop. This is the single most common gotcha.

Allowing a Drop with dragover

The browser blocks dropping unless you call preventDefault() in the dragover handler. Without it, the drop event never fires.

const zone = document.getElementById("dropzone");
zone.addEventListener("dragover", (e) => {
  e.preventDefault(); // REQUIRED to allow drop
});

All lessons in this course

  1. Making Elements Draggable
  2. Drop Targets and dataTransfer
  3. Visual Feedback During Drag
  4. Dragging Files into the Page
← Back to JavaScript Academy