HTML Drag and Drop

Drag and Drop in HTML: Make Elements Move with Purpose

Want to let users drag a banana image into a basket? Reorder a to-do list? Rearrange photos? The HTML Drag and Drop API gives you full control over interactive movement between elements using simple JavaScript events.

How Drag and Drop Works

At its core, drag and drop involves three pieces:

  1. A draggable source element
  2. A target (drop zone)
  3. JavaScript to handle the transfer

Step 1: Enable Dragging

Use the draggable="true" attribute on any HTML element to make it draggable.

<img src="banana.png" id="banana" width="100" draggable="true">

Step 2: Set Up the Drop Zone

You need a container that allows drops. You do this by listening for the dragover and drop events in JavaScript.

<div id="basket" style="width:200px;height:120px;border:2px dashed #aaa;text-align:center;padding-top:40px;">
  Drop banana here
</div>

Step 3: Write JavaScript to Handle Drag and Drop Events

const banana = document.getElementById('banana');
const basket = document.getElementById('basket');

// Start drag
banana.addEventListener('dragstart', (e) => {
  e.dataTransfer.setData('text/plain', 'banana');
});

// Allow drop
basket.addEventListener('dragover', (e) => {
  e.preventDefault(); // Required to allow dropping
});

// Handle drop
basket.addEventListener('drop', (e) => {
  e.preventDefault();
  basket.innerHTML = '🍌 Banana dropped!';
});
[ Drag the banana → Drop it → Message updates in drop zone ]

Customizing Drag Feedback

Use dataTransfer.setDragImage() to change the ghost image shown during drag:

banana.addEventListener('dragstart', (e) => {
  e.dataTransfer.setData('text/plain', 'banana');
  const dragIcon = document.createElement('img');
  dragIcon.src = 'banana.png';
  dragIcon.width = 80;
  e.dataTransfer.setDragImage(dragIcon, 10, 10);
});

Multiple Drop Zones

<div id="basket1">Drop into Basket 1</div>
<div id="basket2">Drop into Basket 2</div>

<script>
function enableDrop(id) {
  const basket = document.getElementById(id);
  basket.addEventListener('dragover', e => e.preventDefault());
  basket.addEventListener('drop', e => {
    e.preventDefault();
    basket.textContent = '🍒 Dropped!';
  });
}
enableDrop('basket1');
enableDrop('basket2');
</script>

Best Practices

  • Always call preventDefault() on dragover to allow dropping
  • Use setData() and getData() to transfer useful info
  • Give visual cues for active drop targets using CSS

Styling Drag Feedback

You can style your drop zone dynamically using dragenter and dragleave events:

basket.addEventListener('dragenter', () => {
  basket.style.background = '#eef';
});
basket.addEventListener('dragleave', () => {
  basket.style.background = '';
});

Summary

Drag and Drop in HTML allows for intuitive, click-free movement of items across the UI. You now know how to:

  • Enable elements to be dragged
  • Design drop zones and capture drop events
  • Customize drag visuals with setDragImage()
  • Style feedback to enhance UX

Check your understanding 1

Which HTML attribute makes an element draggable?
Choose one answer. You can try again.

Answer and explanation

draggable="true"

The correct way to enable dragging on an element is by setting the attribute draggable="true".

Check your understanding 2

The drop event can only be handled by elements that have the attribute droppable="true".
Choose one answer. You can try again.

Answer and explanation

False

There is no droppable attribute in HTML. Drop targets handle the drop event in JavaScript, typically after preventing the default behavior in the dragover event.

Check your understanding 3

Choose the option that lists all and only the correct statements.

  1. dragstart
  2. dragover
  3. drop
  4. dragend
  5. onclick
Which JavaScript events are essential for implementing drag and drop functionality?
Choose one answer. You can try again.

Answer and explanation

Statements 1 and 2 and 3 and 4

The key events for drag and drop are dragstart to initiate dragging, dragover to allow dropping, drop to handle the drop action, and dragend to finalize the drag.

Check your understanding 4

How do you allow an element to accept drop actions in JavaScript?
Choose one answer. You can try again.

Answer and explanation

Call event.preventDefault() inside the dragover event handler

To enable dropping on an element, you must call event.preventDefault() in its dragover event handler. Without this, the drop event won’t fire.

Check your understanding 5

Custom drag feedback images can be set using JavaScript’s setDragImage() method.
Choose one answer. You can try again.

Answer and explanation

True

The setDragImage() method allows developers to customize the drag preview image shown during dragging, improving user experience.

Check your understanding 6

Choose the option that lists all and only the correct statements.

  1. event.dataTransfer.setData()
  2. event.dataTransfer.getData()
  3. event.dataTransfer.clearData()
  4. event.dataTransfer.dropEffect
Which data transfer methods are commonly used in drag and drop operations?
Choose one answer. You can try again.

Answer and explanation

Statements 1 and 2 and 3 and 4

The DataTransfer object methods setData(), getData(), and clearData() manage the data during drag and drop, while dropEffect controls the cursor and action feedback.

What’s Next?

Coming up: Take interactivity to the next level with the Geolocation API — learn how to find a user’s position and use it to personalize their experience.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: HTML Geolocation API →