Reference October 2, 2026

Native Drag-and-Drop is Awesome!

There's a bunch of libraries to facilitate drag-and-drop functionality on the web, but the native functionality built into modern browsers (and actually really well-supported now) is pretty cool and quite simple to use.

The basics

To start making something draggable, simply add draggable="true" to the element:

You will probably want to assign some data to this. To do this, we listen to the dragstart event, and use the event.dataTransfer.setData() method to set some data. You can even set multiple types of data by using various different mime types for the first parameter of that method.

Now we want to allow you to drop somewhere. By listening to the drop event on the target element, we can read out the data using event.dataTransfer.getData().

We can easily make this more user-friendly by adding some styling. We can listen to the dragenter and dragleave events to add a CSS class, showing the user they can drop here:

A custom drag image

If we go back to our simple example, there's another cool thing I want to show: whilst dragging, by default you'll be dragging something that looks exactly like the original element looked like.

Sometimes, however, you might want to show a different preview of the dragging item. You can do so by setting a custom dragImage:

Downloading a file

Sometimes, you might want to drag something out of your browser and into your filesystem. The default action here might be to save a shortcut if the element you're dragging is an <a href>. Google Chrome has a cool trick up its sleeve: it allows you to specify a URL which will be downloaded as a file when dragged into the macOs Finder or Windows Explorer.

If you're using Chrome, try dragging this onto your desktop: