URL: https://www.progressiverobot.com/js-drag-and-drop-vanilla-js/

Introduction

Dragging-and-dropping is a common user interaction that you can find in many graphical user interfaces.

There are pre-existing JavaScript libraries for adding a drag-and-drop feature to your app. However, there may be situations where a library is not available or introduces an overhead or dependency that your project does not need. In these situations, knowledge of the APIs available to you in modern web browsers can offer alternative solutions.

The HTML Drag and Drop API relies on the DOM's event model to get information on what is being dragged or dropped and to update that element on drag or drop. With JavaScript event handlers, you can turn any element into a draggable item or an item that can be dropped into.

In this tutorial, we will build a drag-and-drop example using the HTML Drag and Drop API with vanilla JavaScript to use the event handlers.

Prerequisites

drag illustration for: Prerequisites

To complete this tutorial, you will need:

  • A modern web browser that supports the Drag and Drop API (Chrome 4+, Firefox 3.5+, Safari 3.1+, Edge 18+).

Step 1 — Creating the Project and Initial Markup

Our project will consist of a container with two types of child elements:

  • Child elements that can you can drag
  • Child elements that can have elements dropped into them

First, open your terminal window and create a new project directory:

				
					
mkdir <^>drag-and-drop-example<^>

				
			

Next, navigate to that directory:

				
					
cd <^>drag-and-drop-example<^>

				
			

Then, create an index.html file in that directory:

				
					
nano index.html

				
			

Next, add boilerplate code for a HTML webpage:

				
					
[label index.html]

<!DOCTYPE html>

<html>

 <head>

 <title>My Drag-and-Drop Example</title>

 <link rel="stylesheet" href="style.css" />

 </head>

 <body>

 </body>

</html>

				
			

And between the <body> tags add your <^>draggable<^> item and your <^>dropzone<^> (drop target):

				
					
[label index.html]

&lt;div class="example-parent"&gt;

 &lt;div class="example-origin"&gt;

 &lt;div

 id="draggable-1"

 class="example-draggable"

 &gt;

 draggable

 &lt;/div&gt;

 &lt;/div&gt;



 &lt;div

 class="example-dropzone"

 &gt;

 dropzone

 &lt;/div&gt;

&lt;/div&gt;

				
			

Save and close the file. Then, create a style.css file:

				
					
nano style.css

				
			

Next, add styles for the elements in our index.html file:

				
					
[label style.css]

.example-parent {

 border: 2px solid #DFA612;

 color: black;

 display: flex;

 font-family: sans-serif;

 font-weight: bold;

}



.example-origin {

 flex-basis: 100%;

 flex-grow: 1;

 padding: 10px;

}



.example-draggable {

 background-color: #4AAE9B;

 font-weight: normal;

 margin-bottom: 10px;

 margin-top: 10px;

 padding: 10px;

}



.example-dropzone {

 background-color: #6DB65B;

 flex-basis: 100%;

 flex-grow: 1;

 padding: 10px;

}

				
			

This will add some formatting to the app. Now you can view index.html in the browser and observe that this produces a <^>draggable<^> <div> and a <^>dropzone<^> <div>.

Next, we will explicitly make the first <div> draggable by adding the draggable attribute:

				
					
[label index.html]

&lt;div class="example-parent"&gt;

 &lt;div class="example-origin"&gt;

 &lt;div

 id="draggable-1"

 class="example-draggable"

 &lt;^&gt;draggable="true"&lt;^&gt;

 &gt;

 draggable

 &lt;/div&gt;

 &lt;/div&gt;



 &lt;div

 class="example-dropzone"

 &gt;

 dropzone

 &lt;/div&gt;

&lt;/div&gt;

				
			

Save and close the file.

Finally, view index.html in the browser again. If we click on the <^>draggable<^> <div> and drag it across the screen, there should be a visual indication of it moving.

The default value for the draggable attribute is auto. That means whether the element is draggable will be determined by your browser's default behavior. Typically this means text selections, images, and links are draggable without specifying draggable="true".

You now have an HTML file with a draggable element. We will move on to adding onevent handlers.

Step 2 — Handling Drag-and-Drop Events with JavaScript

Currently, if we release the mouse while dragging the draggable element, nothing happens. To trigger an action on drag or drop on DOM elements, we'll need to utilize the Drag and Drop API:

  • ondragstart: This event handler will be attached to our <^>draggable<^> element and fire when a dragstart event occurs.
  • ondragover: This event handler will be attached to our <^>dropzone<^> element and fire when a dragover event occurs.
  • ondrop: This event handler will also be attached to our <^>dropzone<^> element and fire when a drop event occurs.

Note: There are eight event handlers in total: ondrag, ondragend, ondragenter, ondragexit, ondragleave, ondragover, ondragstart, and ondrop. For our example, we will not require them all.

First, let's reference a new script.js file in our index.html:

				
					
[label index.html]

&lt;body&gt;

 ...

 &lt;^&gt;&lt;script src="script.js"&gt;&lt;/script&gt;&lt;^&gt;

&lt;/body&gt;

				
			

Next, create a new script.js file:

				
					
nano script.js

				
			

The DataTransfer object will keep track of the information related to the current drag happening. To update our element on drag and on drop, we need to directly access the DataTransfer object. To do this, we can select the dataTransfer property from the DOM element's DragEvent.

Note: The DataTransfer object can technically track information for multiple elements being dragged at the same time. For our example, we'll focus on dragging one element.

The dataTransfer object's setData method can be used to set the drag state information for your currently dragged element. It takes two parameters:

  • a string that declares the format of the second parameter
  • the actual data being transferred

Our goal is to move our <^>draggable<^> element to a new parent element. We need to be able to select our <^>draggable<^> element with a unique id. We can set the id of the dragged element with the setData method so it can be used later.

Let's revisit our script.js file and create a new function to use setData:

				
					
[label script.js]

function onDragStart(event) {

 event

 .dataTransfer

 .setData('text/plain', event.target.id);

}

				
			

Note: Internet Explorer 9 through 11 reportedly has problems with using 'text/plain'. The format needs to 'text' for that browser.

To update the dragged item's CSS styling, we can access its styles using the DOM event again and by setting whatever styles we want for the currentTarget.

Let's add to our function and change the backgroundColor to yellow:

				
					
[label script.js]

function onDragStart(event) {

 event

 .dataTransfer

 .setData('text/plain', event.target.id);



 &lt;^&gt;event&lt;^&gt;

 &lt;^&gt;.currentTarget&lt;^&gt;

 &lt;^&gt;.style&lt;^&gt;

 &lt;^&gt;.backgroundColor = 'yellow';&lt;^&gt;

}

				
			

Note: Any styles you change will need to be manually updated again on drop if you want drag-only styles. If you change anything when it starts dragging, the dragged element will keep that new styling unless you change it back.

Now, we have our JavaScript function for when dragging starts.

We can add ondragstart to the <^>draggable<^> element in index.html:

				
					
[label index.html]

&lt;div class="example-parent"&gt;

 &lt;div class="example-origin"&gt;

 &lt;div

 id="draggable-1"

 class="example-draggable"

 draggable="true"

 &lt;^&gt;ondragstart="onDragStart(event);"&lt;^&gt;

 &gt;

 draggable

 &lt;/div&gt;

 &lt;/div&gt;



 &lt;div class="example-dropzone"&gt;

 dropzone

 &lt;/div&gt;

&lt;/div&gt;

				
			

View index.html in your browser. If you try to drag your item now, the styling declared in our function will get applied:

However, nothing will happen when you release your click.

The next event handler fired in this sequence is ondragover.

The default drop behavior for certain DOM elements like <div>s in browsers typically does not accept dropping. This behavior will intercept the behavior we are attempting to implement. To ensure that we get the desired drop behavior, we will apply preventDefault.

Let's revisit the script.js file and create a new function to use preventDefault. Add this code to the end of the file:

				
					
[label script.js]

function onDragOver(event) {

 event.preventDefault();

}

				
			

Now, we can add ondragover to our <^>dropzone<^> element in index.html:

				
					
[label index.html]

&lt;div class="example-parent"&gt;

 &lt;div class="example-origin"&gt;

 &lt;div

 id="draggable-1"

 class="example-draggable"

 draggable="true"

 ondragstart="onDragStart(event);"

 &gt;

 draggable

 &lt;/div&gt;

 &lt;/div&gt;



 &lt;div

 class="example-dropzone"

 &lt;^&gt;ondragover="onDragOver(event);"&lt;^&gt;

 &gt;

 dropzone

 &lt;/div&gt;

&lt;/div&gt;

				
			

At this point, we still have not written code handle the actual dropping. The final event handler fired in this sequence is ondrop.

Let's revisit our script.js file and create a new function.

We can reference the data we saved earlier with dataTransfer object's setData method. We will use dataTransfer object's getData method. The data we set was the id, so that's what will be returned to us:

				
					
[label script.js]

function onDrop(event) {

 const id = event

 .dataTransfer

 .getData('text');

}

				
			

Select our <^>draggable<^> element with the id we retrieved:

				
					
[label script.js]

function onDrop(event) {

 // ...



 &lt;^&gt;const draggableElement = document.getElementById(id);&lt;^&gt;

}

				
			

Select our <^>dropzone<^> element:

				
					
[label script.js]

function onDrop(event) {

 // ...



 &lt;^&gt;const dropzone = event.target;&lt;^&gt;

}

				
			

Append our <^>draggable<^> element to the <^>dropzone<^>:

				
					
[label script.js]

function onDrop(event) {

 // ...



 &lt;^&gt;dropzone.appendChild(draggableElement);&lt;^&gt;

}

				
			

Reset our dataTransfer object:

				
					
[label script.js]

function onDrop(event) {

 // ...



 &lt;^&gt;event&lt;^&gt;

 &lt;^&gt;.dataTransfer&lt;^&gt;

 &lt;^&gt;.clearData();&lt;^&gt;

}

				
			

Now, we can add ondrop to our <^>dropzone<^> element in index.html:

				
					
[label index.html]

&lt;div class="example-parent"&gt;

 &lt;div class="example-origin"&gt;

 &lt;div

 id="draggable-1"

 class="example-draggable"

 draggable="true"

 ondragstart="onDragStart(event);"

 &gt;

 draggable

 &lt;/div&gt;

 &lt;/div&gt;



 &lt;div

 class="example-dropzone"

 ondragover="onDragOver(event);"

 &lt;^&gt;ondrop="onDrop(event);"&lt;^&gt;

 &gt;

 dropzone

 &lt;/div&gt;

&lt;/div&gt;

				
			

Once that's done, we have a completed drag-and-drop feature. View index.html in your browser and drag the <^>draggable<^> element to the <^>dropzone<^>.

Our example handles the scenario of a single draggable item and a single drop target. You can have multiple draggable items, multiple drop targets, and customize it with all the other Drag and Drop API event handlers.

Step 3 — Building an Advanced Example with Multiple Draggable Items

Here's one more example of how you could use this API: a to-do list with draggable tasks that you can move from a "To-do" column to a "Done" column.

To create your own to-do list, add more draggable elements with unique ids to index.html:

				
					
[label index.html]

&lt;div class="example-parent"&gt;

 &lt;h1&gt;To-do list&lt;/h1&gt;

 &lt;div class="example-origin"&gt;

 To-do

 &lt;div

 id="draggable-1"

 class="example-draggable"

 draggable="true"

 ondragstart="onDragStart(event);"

 &gt;

 thing 1

 &lt;/div&gt;

 &lt;^&gt;&lt;div&lt;^&gt;

 &lt;^&gt;id="draggable-2"&lt;^&gt;

 &lt;^&gt;class="example-draggable"&lt;^&gt;

 &lt;^&gt;draggable="true"&lt;^&gt;

 &lt;^&gt;ondragstart="onDragStart(event);"&lt;^&gt;

 &lt;^&gt;&gt;&lt;^&gt;

 &lt;^&gt;thing 2&lt;^&gt;

 &lt;^&gt;&lt;/div&gt;&lt;^&gt;

 &lt;^&gt;&lt;div&lt;^&gt;

 &lt;^&gt;id="draggable-3"&lt;^&gt;

 &lt;^&gt;class="example-draggable"&lt;^&gt;

 &lt;^&gt;draggable="true"&lt;^&gt;

 &lt;^&gt;ondragstart="onDragStart(event);"&lt;^&gt;

 &lt;^&gt;&gt;&lt;^&gt;

 &lt;^&gt;thing 3&lt;^&gt;

 &lt;^&gt;&lt;/div&gt;&lt;^&gt;

 &lt;^&gt;&lt;div&lt;^&gt;

 &lt;^&gt;id="draggable-4"&lt;^&gt;

 &lt;^&gt;class="example-draggable"&lt;^&gt;

 &lt;^&gt;draggable="true"&lt;^&gt;

 &lt;^&gt;ondragstart="onDragStart(event);"&lt;^&gt;

 &lt;^&gt;&gt;&lt;^&gt;

 &lt;^&gt;thing 4&lt;^&gt;

 &lt;^&gt;&lt;/div&gt;&lt;^&gt;

 &lt;/div&gt;



 &lt;div

 class="example-dropzone"

 ondragover="onDragOver(event);"

 ondrop="onDrop(event);"

 &gt;

 Done

 &lt;/div&gt;

&lt;/div&gt;

				
			

View index.html in your browser and drag the items in the To-do column to the Done column. You have created a to-do application and tested the functionality.

Conclusion

In this article, you created a to-do app to explore the drag-and-drop functionality that is available to modern web browsers.

The Drag and Drop API provides multiple options for customizing your actions beyond dragging and dropping. For example, you can update the CSS styling of your dragged items. Also, instead of moving the item, you can choose to copy your draggable item so that it gets replicated on drop.

Bear in mind that while many web browsers support this technology, you may not be able to rely on it if your audience consists of devices that do not support this functionality.

To learn more about all you can drop with the Drag and Drop API, check out MDN's docs on it.