Smartoolz

HTML Drag and Drop

HTML Drag and Drop API

HTML Drag and Drop allows users to drag elements and drop them into another area.

What is Drag and Drop?

Drag and Drop is a browser feature used for moving elements visually.

It is commonly used in:

Make an Element Draggable

HTML Example

Drag Me

Important Events

Event Description
dragstart Triggered when dragging starts
dragover Triggered while dragging over target
drop Triggered after dropping

Live Demo

Drag the green box into another box.

Drag Me

JavaScript Code

function allowDrop(event){

event.preventDefault();

}

function drag(event){

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

}

function drop(event){

event.preventDefault();

let data =
event.dataTransfer.getData("text");

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

}

Benefits of Drag and Drop