Articles in this section
Category / Section

How to use drag and drop ui in JavaScript?

2 mins read

EJ2 Drag and Drop UI libraries

 

This documentation explains how to use Drag and Drop UI libraries in JavaScript.

Essential JS 2 Drag and Drop UI libraries

 

Drag and Drop is supported through two library modules: Draggable and Droppable. Draggable module makes DOM to be dragged using mouse or by touch gestures, and Droppable module mark requires DOM as droppable zone.

Installing

 

You can get both the Drag and Drop Essential JS 2 UI Library modules from a single npm package named “@syncfusion/ej2-base”.

https://www.npmjs.com/package/@syncfusion/ej2-base

You can run the following command to install.

 

                                                                  npm install @syncfusion/ej2-base

 

 

Initializing draggable

 

You can make any element draggable by passing the element to Draggable constructor. Refer to the following code snippet to enable draggable for DOM element.

 

import {Draggable} from  '@syncfusion/ej2-base';

 

 let dragElement: HTMLElement = document.getElementById('element1');

 

 let draggable:Draggable = new Draggable(dragElement,{clone: false});

 

Creating droppable zone

 

You can convert any DOM element as a droppable zone, which accepts the draggable elements. Refer to the following code snippet to enable droppable zone.

 

import { Droppable} from  '@syncfusion/ej2-base';

 

let droppable: Droppable = new Droppable(document.getElementById('droppable'));

 

Defining drop action

 

To define the drop action, set drop callback function during droppable object creation. You can get details of dropped element using the dropped element property in event argument. Refer to the following code snippet to use basic Drag and Drop action.

 

import { Draggable, Droppable, DropEventArgs } from '@syncfusion/ej2-base';

 

let draggable: Draggable = new Draggable(document.getElementById('element1'), {

 

    clone: false

 

});

 

let droppable: Droppable = new Droppable(document.getElementById('droppable'), {

 

   drop: (e: DropEventArgs) => {

 

        e.droppedElement.querySelector('.drag-text').textContent = 'Dropped';

 

    }

 

});

 

 

Sample:

 

https://github.com/SyncfusionExamples/EJ2-Drag-and-Drop-UI-libraries

 

Resources

You can also refer to the following resources to learn more details about Essential JS 2 components.

Conclusion

I hope you enjoyed learning about how to use drag and drop ui in JavaScript.

You can refer to our JavaScript feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our JavaScript example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied