Skip to content
Advertisement

How to Make This Drag n Droppable

I’m trying to make a form element drag and droppable, but I can’t make it work. Any ideas? I’m trying to make it be able to be dropped into another element.

Here is the code for the form element:

JavaScript

Advertisement

Answer

The code below is based on the HTML5 Drag and Drop API. When you drop it into a box, it will execute a function that will drop it there.

JavaScript
JavaScript
JavaScript

I added IDs because they are required for it to work.

This code adds two boxes that you can drag and drop an element into. It can also be dropped between pages.

Since a form input is also classified as something that can have stuff dropped into, I wouldn’t recommend drag and dropping a form with an input. You will get a hierarchy error if someone miss-clicks, and the DnD will refuse to work until you reload.

More information is on these pages: MDN webdev

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement