Skip to main content
HTML for Beginners
CHAPTER 17 Beginner

HTML Drag and Drop API

Updated: May 10, 2026
5 min read

# HTML Drag and Drop API

1. Introduction

HTML5 introduced native Drag and Drop (DnD). This API allows you to click an element, drag it across the screen, and drop it into another element. It's the foundation of modern interfaces like Trello boards or file-upload zones.

2. Learning Objectives

  • Make an element draggable.
  • Create a drop zone.
  • Understand the basic JavaScript events required (ondragstart, ondragover, ondrop).

3. Detailed Explanations

Step 1: Make it Draggable

To make an HTML element draggable, set the draggable attribute to true.
html
1
<img id="drag1" src="logo.png" draggable="true" ondragstart="drag(event)">

Step 2: What to Drag (ondragstart)

When the user starts dragging, JavaScript stores the ID of the dragged item in the DataTransfer object.
javascript
1234
function drag(ev) {
    // Save the ID of the element being dragged
    ev.dataTransfer.setData("text", ev.target.id);
}

Step 3: Where to Drop (ondragover)

By default, HTML elements cannot be dropped onto other elements. To create a "Drop Zone", you must prevent this default behavior using ondragover.
html
1234567
<div id="div1" ondrop="drop(event)" ondragover="allowDrop(event)"></div>

<script>
function allowDrop(ev) {
    ev.preventDefault(); // Allows dropping
}
</script>

Step 4: Do the Drop (ondrop)

When the item is released over the drop zone, retrieve the ID and append the element to the new container.
javascript
1234567
function drop(ev) {
    ev.preventDefault();
    // Get the ID of the dragged item
    var data = ev.dataTransfer.getData("text");
    // Append the item to the drop zone
    ev.target.appendChild(document.getElementById(data));
}

4. Mini Project: Task Board UI (Kanban)

html
12345678910111213141516
<style>
.column { width: 200px; min-height: 300px; border: 1px solid #ccc; padding: 10px; float: left; margin: 10px; background: #f9f9f9; }
.task { background: #fff; border: 1px solid #999; padding: 10px; margin-bottom: 10px; cursor: grab; }
</style>

<!-- Drop Zones -->
<div class="column" ondrop="drop(event)" ondragover="allowDrop(event)">
    <h3>To Do</h3>
    <!-- Draggable Task -->
    <div class="task" id="task1" draggable="true" ondragstart="drag(event)">Task 1</div>
    <div class="task" id="task2" draggable="true" ondragstart="drag(event)">Task 2</div>
</div>

<div class="column" ondrop="drop(event)" ondragover="allowDrop(event)">
    <h3>Done</h3>
</div>

5. MCQs

Q1: Which attribute makes an HTML element draggable? A) drag="true" B) moveable="true" C) draggable="true" D) drag-and-drop="enable" *Answer: C*

6. Summary

The HTML Drag and Drop API combines the draggable HTML attribute with specific JavaScript events (dragstart, dragover, drop) to create highly interactive, modern web applications.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·