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.
<div draggable="true">
Move me!
</div>
element.addEventListener(
"dragstart", handler
);
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
✅ Completed
Move a card to try it!
Meet the draggable Attribute
To make an ordinary HTML element draggable with the native API, set draggable="true".
<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.
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.
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.
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.
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.
Make a Container Accept Drops
For an element to accept a drop, you usually need to cancel the default behavior of the dragover event.
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.
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.
.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.
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.
<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>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
- Create two columns labeled To Do and Completed.
- Add at least three task cards with unique IDs.
- Make each card draggable with
draggable="true". - Use
dragstartto store the dragged card’s ID. - Use
dragoveranddropto move cards between columns. - Provide a button-based way to move cards without dragging.
- Display a message when a task moves.
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.
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
draggableattribute 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 CompletedKeep 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!


