How to use drag and drop ui in JavaScript?
This article 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 enables DOM elements to be dragged using a mouse or touch gestures, and the Droppable module designates DOM elements as droppable zones.
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 the Draggable constructor. Refer to the following code snippet to enable draggable for a 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 a 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.
- https://ej2.syncfusion.com/demos/#/material/grid/drag-and-drop.html
- https://ej2.syncfusion.com/documentation/common/drag-and-drop/
Conclusion
We 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 forums, BoldDesk Support, or feedback portal. We are always happy to assist you!