Draggable Element Using JavaScript Last Updated : 12 Mar, 2025 Comments Improve Suggest changes Like Article Like Report Creating a draggable element and moving it to another place within the page is a very interactive and user-friendly concept that makes it easier for the user. This feature allows the user to click and hold the mouse button over a div, drag it to another location, and release the mouse button to drop the element.Let us have a look at what the final feature will look like:Draggable ElementsApproachWe will make this draggable feature to drag any element. Here is the step-by-step approach to creating a draggable element.HTML Section: The HTML section of the page will be inside the index.html file, and we are only creating a div for this project. You can take any element to make it draggable.CSS Section: This section will contain all the code related to the styling of the page or the div that we have taken for this project. The code of this file is given below.JavaScript Section: This section handles JavaScript functions like onMouseDrag(). Example: Write the below code in the respective files. HTML <html> <head> <style> .draggable { width: 200px; height: 200px; margin: 10px; background-color: #f39c12; position: absolute; cursor: pointer; padding: 10px; border: 2px solid #333; } </style> </head> <body> <div id="div1" class="draggable" style="top: 50px; left: 50px;">Div 1</div> <div id="div2" class="draggable" style="top: 100px; left: 300px;">Div 2</div> <div id="div3" class="draggable" style="top: 200px; left: 500px;">Div 3</div> <script> const dragElements = document.querySelectorAll(".draggable"); function onMouseDrag(event, element) { let leftValue = parseInt(window.getComputedStyle(element).left); let topValue = parseInt(window.getComputedStyle(element).top); element.style.left = `${leftValue + event.movementX}px`; element.style.top = `${topValue + event.movementY}px`; } dragElements.forEach((element) => { element.addEventListener("mousedown", (e) => { const onMove = (event) => onMouseDrag(event, element); document.addEventListener("mousemove", onMove); document.addEventListener("mouseup", () => { document.removeEventListener("mousemove", onMove); }, { once: true }); }); }); </script> </body> </html> Output: In this exampleThe script selects all elements with the class .draggable using document.querySelectorAll(".draggable").When you click on any .draggable element (mousedown event), the script begins tracking the mouse movement.As you drag, the mousemove event updates the position of the dragged element by adjusting its left and top styles.When you release the mouse button (mouseup event), the script stops moving the element by removing the mousemove event listener.Each .draggable element is independently draggable, so clicking and dragging one element won’t affect others. Comment More infoAdvertise with us Next Article Draggable Element Using JavaScript R rahulkwh Follow Improve Article Tags : HTML JavaScript-Projects Similar Reads script.aculo.us Sorting Elements Sortable provides the ability to user to create many draggable elements within the container. When you create a Sortable, it automatically creates the corresponding Draggable and Droppable for the container. Syntax:  Sortable.create('container_id', {options}); Sortable Options: OptionsDescriptionta 2 min read jQuery UI Draggable drag Event jQuery UI is a web-based technology and consists of various GUI widgets, visual effects, and themes. These features can be implemented using the jQuery JavaScript Library. jQuery UI is the best tool for building UI interfaces for the webpages. It can also be used to build highly interactive web appl 2 min read jQuery UI Draggable stop Event In this article, we are going to learn that how we can use the jQuery UI Draggable stop event. Draggable will be used to drag the HTML element throughout the webpage. jQuery UI Draggable consists of options, methods, and events. The stop is one example of a Draggable event. Now let's understand what 3 min read jQuery UI Draggable start Event jQuery UI consists of GUI widgets, visual effects, and themes implemented using jQuery, CSS, and HTML. jQuery UI is great for building UI interfaces for the webpages. In this article, we are going to understand the jQuery UI Draggable start event. We can drag any HTML element throughout the page. Sy 2 min read jQuery UI Draggable enable() Method jQuery UI consists of GUI widgets, visual effects, and themes implemented using jQuery, CSS, and HTML. jQuery UI is great for building UI interfaces for the webpages. The jQuery UI Draggable enable() method is used to enable the draggable option. This method does not accept any parameter. Syntax: $( 1 min read jQuery UI Draggable option() Method jQuery Mobile is a web-based technology that can be used to make responsive content for websites that can be accessed on all types of smartphones, tablets, and desktops. In this article, we are going to learn the jQuery Mobile Draggable option() method. Using this method, we can get, set or update a 3 min read jQuery UI Draggable widget() Method jQuery UI is a web-based technology and consists of GUI widgets, visual effects, and themes implemented using the jQuery, JavaScript Library. jQuery UI is the best tool for building UI interfaces for the webpages. It can also be used to build highly interactive web applications or can be used to add 2 min read jQuery UI Draggable iframeFix Option jQuery UI is a web-based technology and consists of GUI widgets, visual effects, and themes implemented using the jQuery, JavaScript Library. jQuery UI is the best tool for building UI interfaces for the webpages. It can also be used to build highly interactive web applications or can be used to ad 2 min read jQuery UI Draggable instance() Method jQuery UI is a web-based technology and consists of various GUI widgets, visual effects, and themes. These features can be implemented using the jQuery JavaScript library. jQuery UI is the best tool for building UI interfaces for the webpages. It can also be used to build highly interactive web appl 2 min read jQuery UI Draggable stack Option jQuery UI is a web-based technology and consists of GUI widgets, visual effects, and themes implemented using the jQuery, JavaScript Library. jQuery UI is the best tool for building UI interfaces for the webpages. It can also be used to build highly interactive web applications or can be used to add 2 min read Like