Drag and Drop

Drag and Drop is a feature introduced in HTML5 that allows users to click, drag, and drop elements on a web page, enabling more interactive and intuitive user interfaces. To create a drag and drop interface, you need to add event listeners for various drag-related events and define the behavior for each event.

Here's a simple example of how to create a basic drag and drop interface:

<!DOCTYPE html>
    .draggable {
      width: 100px;
      height: 100px;
      background-color: blue;
      cursor: move;

    .dropzone {
      width: 200px;
      height: 200px;
      border: 1px solid black;
  <div id="draggable" class="draggable" draggable="true" ondragstart="dragStart(event)"></div>
  <div id="dropzone" class="dropzone" ondragover="allowDrop(event)" ondrop="drop(event)"></div>

    function dragStart(event) {

    function allowDrop(event) {

    function drop(event) {
      var data = event.dataTransfer.getData("text");;

In this example, there are two elements: a draggable div and a drop-zone div. The draggable div has the `draggable` attribute set to `true`, which makes it draggable by default.

The `dragStart(event)` function is called when the user starts dragging the element. The `setData` method is used to store the ID of the dragged element in the `dataTransfer` object.

The `allowDrop(event)` function is called when a draggable element is dragged over the drop-zone. By calling `event.preventDefault()`, the default behavior (not allowing the element to be dropped) is overridden, allowing the element to be dropped.

The `drop(event)` function is called when the draggable element is dropped in the drop-zone. The `getData` method retrieves the ID of the dragged element from the `dataTransfer` object, and the `appendChild` method is used to add the dragged element to the drop-zone.

By utilizing the drag and drop API, you can create more interactive and user-friendly web applications, making it easier for users to interact with your content.

Was this page helpful?