JavaScript/jQuery Draggable - Getting Started

NOTE
Before you start the tutorial, ensure DevExtreme is installed in your application.

This tutorial guides you through the following steps:

  • Add a Draggable to a page.
  • Configure Draggable options.
  • Handle events.

Each section in this tutorial covers a single configuration step. You can find the complete source code in the following GitHub repository:

View on GitHub

Create Draggable

Add DevExtreme to your jQuery application and use the following code to create a Draggable:

index.js
index.html
  • $(function() {
  • $("#draggable").dxDraggable({});
  • });
  • <html>
  • <head>
  • <!-- ... -->
  • <script type="text/javascript" src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
  • <link rel="stylesheet" href="https://cdn3.devexpress.com/jslib/26.1.6/css/dx.fluent.blue.light.css">
  • <script type="text/javascript" src="https://cdn3.devexpress.com/jslib/26.1.6/js/dx.all.js"></script>
  • <script type="text/javascript" src="index.js"></script>
  • </head>
  • <body class="dx-viewport">
  • <div id="draggable"></div>
  • </body>
  • </html>

Draggable does not include default visual elements. Specify custom markup as follows:

index.js
index.html
  • $(function() {
  • $("#note-1").dxDraggable({});
  • });
  • <html>
  • <head>
  • <!-- ... -->
  • </head>
  • <body class="dx-viewport">
  • <div id="note-1" class="note">
  • <div class="color-indicator blue"></div>
  • <div class="text-container">
  • <div class="body-text-box">Install New Router in Dev Room</div>
  • <div class="detail-text-box">Amelia Harper</div>
  • </div>
  • </div>
  • </body>
  • </html>

This example creates four Draggable components inside a common .board container:

index.js
index.html
  • $(function() {
  • $("#note-1").dxDraggable({});
  • $("#note-2").dxDraggable({});
  • $("#note-3").dxDraggable({});
  • $("#note-4").dxDraggable({});
  • });
  • <div class="board">
  • <div id="note-1" class="note">
  • <div class="color-indicator blue"></div>
  • <div class="text-container">
  • <div class="body-text-box">Install New Router in Dev Room</div>
  • <div class="detail-text-box">Amelia Harper</div>
  • </div>
  • </div>
  •  
  • <div id="note-2" class="note">
  • <div class="color-indicator green"></div>
  • <div class="text-container">
  • <div class="body-text-box">👨‍💻 Launch New Website</div>
  • <div class="detail-text-box">Brett Wade</div>
  • </div>
  • </div>
  •  
  • <div id="note-3" class="note">
  • <div class="color-indicator red"></div>
  • <div class="text-container">
  • <div class="body-text-box">Prepare 2026 Marketing Plan</div>
  • <div class="detail-text-box">Robert Reagan</div>
  • </div>
  • </div>
  •  
  • <div id="note-4" class="note">
  • <div class="color-indicator yellow"></div>
  • <div class="text-container">
  • <div class="body-text-box">🖥️ Approve Personal Computer Upgrade Plan</div>
  • <div class="detail-text-box">Bart Arnaz</div>
  • </div>
  • </div>
  • </div>

Configure Draggable Options

This example specifies the following Draggable properties:

  • boundary: Constrains Draggable movement inside a container (.board).
  • group: Specifies an interaction group (used in events such as onDragMove).

Handle Events

This example specifies an onDragStart handler to update the note's Z-index (bring to front):

index.js
  • $(function() {
  • let z = 1;
  •  
  • $("#note-1").dxDraggable({
  • onDragStart(e) {
  • changeZIndex($(e.element[0]));
  • }
  • // ...
  • });
  •  
  • function changeZIndex(el) {
  • el.css("z-index", z);
  • z++;
  • }
  • });

This example also specifies handlers for four common events (not specific to Draggable):

  • click
  • dxdragenter
  • dxdragleave
  • dxdrop

To attach handlers to common events, call Draggable.on():

index.js
index.css
  • $(function() {
  • $("#note-1").dxDraggable({
  • group: 'notes',
  •  
  • // ...
  • }).on({
  • "click": handleClick,
  • "dxdragenter": handleDragEnter,
  • "dxdragleave": handleDragStop,
  • "dxdrop": handleDragStop,
  • });
  •  
  • // ...
  •  
  • function handleClick(e) {
  • changeZIndex($(e.currentTarget));
  • }
  •  
  • function handleDragEnter(e) {
  • e.target.classList.add('overlapped');
  • }
  •  
  • function handleDragStop(e) {
  • e.target.classList.remove('overlapped');
  • }
  • });
  • .note.overlapped {
  • outline: 1px dashed var(--dx-color-primary);
  • }
NOTE
Specify an identical group value for all Draggable instances to ensure the components interact with each other (in dxdragenter, dxdragleave, and dxdrop handlers).

Refer to the following repository for the complete source code of this tutorial:

View on GitHub