mc0de
Advanced HTML · Lesson 09 of 13

Drag and Drop

Make your websites more interactive! Learn how to move items between containers, respond to drag events, and build a simple task board using HTML and JavaScript.

Drag & Drop API JavaScript Events Drop Zones Interactive Challenge
● ● ●   drag-and-drop.html
<div draggable="true">
  Move me!
</div>

element.addEventListener(
  "dragstart", handler
);
To Do
Write HTML ↗
Completed
✓ Learn Forms
01 / Getting Started

Move Things Around With Drag and Drop!

Imagine moving a task from “To Do” to “Completed” simply by dragging it across the screen. Or rearranging cards in a dashboard. That’s what the HTML Drag and Drop API can help you build.

Try this little task board! Drag a card between columns, or use its button to move it.

📋 To Do

Practice HTML
Build a project

✅ Completed

Move a card to try it!

Did you notice? Drag and drop is an interaction. HTML defines the elements, and JavaScript controls what happens when you move them.
02 / Draggable Elements

Meet the draggable Attribute

To make an ordinary HTML element draggable with the native API, set draggable="true".

A draggable element
<div id="task1" draggable="true">
  Drag this task!
</div>

But there’s an important detail: making something draggable doesn’t automatically tell the browser where to put it. You need JavaScript event listeners to handle the movement.

03 / Drag Events

Understand the Drag Lifecycle

The browser fires several events while someone drags an element. The most useful are:

  • dragstart — the drag begins.
  • dragover — the dragged item moves over a possible target.
  • drop — the item is released on an accepted target.
  • dragend — the drag operation finishes.
Listening for drag events
const task = document.getElementById("task1");

task.addEventListener("dragstart", function(event) {
  console.log("Dragging started!");
});

task.addEventListener("dragend", function() {
  console.log("Dragging finished!");
});

Think of these events as checkpoints. They let your code respond at different moments during a drag operation.

04 / DataTransfer

Tell the Drop Zone What’s Moving

During a drag operation, the browser provides a dataTransfer object. You can use it to pass an identifier for the item being dragged.

Passing the task ID
task.addEventListener("dragstart", function(event) {
  event.dataTransfer.setData("text/plain", task.id);
  event.dataTransfer.effectAllowed = "move";
});

Later, the drop handler can read the ID using getData("text/plain") and find the matching element.

Security tip: Treat dropped data as untrusted. For a task board, check that the ID belongs to one of your expected draggable elements before moving anything.
05 / Drop Zones

Make a Container Accept Drops

For an element to accept a drop, you usually need to cancel the default behavior of the dragover event.

Allowing a drop
const zone = document.getElementById("completed");

zone.addEventListener("dragover", function(event) {
  event.preventDefault();
});

zone.addEventListener("drop", function(event) {
  event.preventDefault();

  const id = event.dataTransfer.getData("text/plain");
  const task = document.getElementById(id);

  if (task && task.classList.contains("task")) {
    zone.appendChild(task);
  }
});

The appendChild() method moves an existing element to its new parent. It doesn’t create a duplicate.

06 / Visual Feedback

Show Visitors Where to Drop

Good drag-and-drop interfaces give visual feedback. You might highlight a drop zone when a draggable item enters it, then remove the highlight afterward.

Highlighting a drop zone
.drop-zone {
  border: 2px dashed #64748b;
  padding: 20px;
}

.drop-zone.over {
  border-color: #4ee4a1;
  background: #e1fff0;
}

JavaScript can add or remove the over class in response to drag events. A clear target makes the interface easier to understand.

07 / Accessibility & Mobile

Don’t Make Dragging the Only Option

Native HTML drag-and-drop behavior can vary across touch devices and is not an accessible interaction by itself.

For a task board, an excellent alternative is a button that moves the same card without requiring a drag gesture.

An alternative move action
<button type="button" id="moveTask">
  Move task to Completed
</button>

<script>
document.getElementById("moveTask")
  .addEventListener("click", function() {
    document.getElementById("completed")
      .appendChild(document.getElementById("task1"));
  });
</script>
Best practice: Support mouse, keyboard, and touch users with clear alternative controls. Use a status message to announce when an item has moved.
🚀 mC0de Challenge

Build a Drag-and-Drop Task Board

Create a simple project board that helps you organize tasks. Your visitors should be able to move cards between columns and see their progress.

Your Mission

  1. Create two columns labeled To Do and Completed.
  2. Add at least three task cards with unique IDs.
  3. Make each card draggable with draggable="true".
  4. Use dragstart to store the dragged card’s ID.
  5. Use dragover and drop to move cards between columns.
  6. Provide a button-based way to move cards without dragging.
  7. Display a message when a task moves.
Extra challenge: Add a third column named “In Progress” and let users move tasks among all three columns!
💻 Interactive HTML Sandbox

Build Your Task Board!

Start by dragging a task between columns. Then improve the code by adding more cards, alternative move buttons, and status messages. Click Run Code to test your changes.

Live Result
Lesson 09 Recap

What You’ve Learned

You’re Building Interactive Interfaces!

You’ve learned how to move beyond static pages and create interfaces that respond to user actions.

  • How the draggable attribute works.
  • How to handle drag events with JavaScript.
  • How to transfer an item ID between drag and drop handlers.
  • How to move elements between containers.
  • How to provide accessible alternatives to dragging.

Next up: Local Storage — learn how to save information in the browser so it can remain available after a page refresh!

Your Learning Journey

0 / 13 Completed

Keep going! Every completed lesson brings you closer to mastering Advanced HTML.

🎉

Nice Work!

You've learned how to create draggable elements, handle drag events, build drop zones, and provide accessible ways to move tasks. Your web interfaces are getting more interactive!