Debug School

rakesh kumar
rakesh kumar

Posted on

Implement drag and droppable functionality in jquery

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Drag and Drop Example</title>
    <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
    <script src="https://code.jquery.com/jquery-3.6.4.min.js"></script>
    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
    <style>
        #todo, #inprogress {
            min-height: 100px;
            border: 1px solid #ddd;
            margin: 10px;
            padding: 10px;
            float: left;
        }
        .card {
            padding: 10px;
            margin: 5px;
            cursor: move;
            background-color: #f0f0f0;
        }
    </style>
</head>
<body>

<div id="todo" class="container">
    <div class="card" id="task1">Task 1</div>
    <div class="card" id="task2">Task 2</div>
    <!-- Add more cards for your "To-Do" list -->
</div>

<div id="inprogress" class="container">
    <!-- "In Progress" cards will be added here -->
</div>

<script>
    $(document).ready(function() {
        $(".card").draggable({
            revert: "invalid",
            helper: "clone",
            cursor: "move"
        });

        $("#inprogress").droppable({
            accept: ".card",
            drop: function(event, ui) {
                var droppedCard = $(ui.helper).clone();
                $(droppedCard).removeClass("ui-draggable-dragging");
                $(this).append(droppedCard);
            }
        });
    });
</script>

</body>
</html>
Enter fullscreen mode Exit fullscreen mode

This code sets up a simple HTML structure with two containers ("To-Do" and "In Progress") and uses jQuery UI for drag-and-drop functionality. The .draggable() function is applied to the cards, and the revert, helper, and cursor options are specified. The #inprogress container is set as droppable using .droppable(), specifying that it accepts cards with the class .card. When a card is dropped into the "In Progress" container, a clone of the card is created and appended to the container.

droppable
jquery-ui-droppable
draggable-and-droppable-methods

Top comments (0)