logo Learn HTML
  • Home
  • Tutorials
  • Practice
  • Quiz
  • Projects
  • About
  • Contact

HTML Course

  • HTML HOME
  • HTML Introduction
  • HTML Basic
  • HTML Element
  • HTML Attributes
  • HTML Headings
  • HTML Paragrapha
  • HTML Style
  • HTML Formattin
  • HTML Quotations
  • HTML Colors
  • HTML Css
  • HTML Links
  • HTML Image
  • HTML Favicon
  • Page Tittle
  • HTML Tables
  • HTML Lists
  • Block & Inline
  • HTML Div
  • HTML Classes
  • HTML Id
  • HTML Buttons
  • HTML Iframes
  • HTML File Paths
  • HTML Head
  • HTML Layout
  • HTML Symbols
  • HTML Emojis
  • HTML URL Encode
  • HTML VS XHTML
  • HTML FROM
  • HTML Form
  • Form Attributes
  • Form Elements
  • HTML Input Types
  • HTML Input Attributes
  • Input Form Attributes
  • HTML Graphics
  • HTML Canvas
  • HTML SVG
  • HTML Media
  • HTML Media
  • HTML Video
  • HTML Audio
  • HTML Plug-ins
  • HTML Youtube
  • HTML APIS
  • HTML Web APIS
  • HTML Geolocation
  • HTML Drag & Drop
  • Web Storage
  • Web Workers
  • HTML SSE
  • HTML Examples
  • HTML Examples
  • HTML Editor
  • HTML Quiz
  • HTML Exercises
  • HTML References
  • HTML Tag List
  • HTML Attributes
  • HTML Global Attributes
  • HTML Brower Support
  • HTML Events
  • HTML Colors
  • HTML Canvas
  • HTML Audio & Video
  • HTML Doctypes
  • HTML Character Sets
  • HTML URL Encode
  • Language Codes
  • HTTP Messages
  • HTTP Methods
  • PX to EM Converter
  • keyboard Shortcuts

Mastering the HTML Drag and Drop API

Drag and drop is a familiar user interface concept where you "grab" an object and move it to a new location. With the HTML Drag and Drop API, you can implement this feature natively in the browser.


1. Browser Compatibility

Modern browsers provide excellent support for this API:

API Chrome Edge Firefox Safari Opera
Drag & Drop 4.0+ 9.0+ 9.0+ 6.0+ 12.0+

2. How to Implement Drag and Drop

Building a drag-and-drop interaction involves three main steps: making an object moveable, defining the data to move, and setting a valid drop zone.


Step A: Make an Element Draggable

By default, most HTML elements are not draggable. You must add the draggable="true" attribute.

HTML

<img id="myImage" src="logo.png" draggable="true">

<p id="textBlock" draggable="true"> You can move this text! <p>


Step B: Identify the Data (ondragstart)

When the user starts dragging, you need to store the ID of the element so the browser knows what is being moved. We use the dataTransfer.setData() method for this.

Javascript

function startDragging(event) {

// Store the ID of the element being dragged

event.dataTransfer.setData("textID", event.target.id);

}


Step C: Define the Drop Zone (ondragover)

Browsers naturally prevent dropping data into other elements. To allow a drop, we must cancel the default behavior using preventDefault().

Javascript

function allowDrop(event) {

event.preventDefault(); // This allows the drop to happen

}


Step D: Process the Drop (ondrop)

When the user releases the mouse, we retrieve the stored ID and move the element to the new container.

HTML

function handleDrop(event) {

event.preventDefault();

// Retrieve the ID we stored earlier

const elementID = event.dataTransfer.getData("textID");

// Move the element into the new container

event.target.appendChild(document.getElementById(elementID));

}


3. Complete Working Example

Copy and paste this code to see a functional drag-and-drop demo.

Javascript

<!DOCTYPE html>

<html>

<head>

<style>

.drop-box {
width: 350px;
height: 100px;
padding: 10px;
border: 2px dashed #ccc;
margin-bottom: 20px
}

</style>

<script>

function startDragging(ev) {

ev.dataTransfer.setData("elementID", ev.target.id);

}


function allowDrop(ev) {

ev.preventDefault();

}


function handleDrop(ev) {

ev.preventDefault();

const id = ev.dataTransfer.getData("elementID");

ev.target.appendChild(document.getElementById(id));

}

</script>

</head>

<body>

<h3> Drag the elements into the box: </h3>

<div class="drop-box" ondrop="handleDrop(event)" ondragover="allowDrop(event)"> <div>

<img id="dragImg" src="https://via.placeholder.com/150/0000FF/808080?Text=DragMe"
draggable="true" ondragstart="startDragging(event)" width="100">

<h2> Move this Heading! <h2>

</body>

</html>


Summary Checklist for Developers

  • Draggable: Set draggable="true" on the source

  • Data Storage: Use event.dataTransfer.setData() inside the ondragstart event.

  • Enable Drop: Call event.preventDefault() inside the ondragover event.

  • Finalize: Use event.target.appendChild() inside the ondrop event to complete the move.

Try It Your Self

Sign Up For New
Update

HTML Basic
  1. HTML Basic
  2. HTML Element
  3. HTML Attributes
  4. HTML Headings
  5. HTML Paragrapha
  6. HTML Tables
  7. HTML Lists
HTML Advance
  1. HTML Div
  2. HTML Classes
  3. HTML Id
  4. HTML Buttons
  5. HTML Iframes
  6. HTML Symbols
  7. HTML Emojis