in Education by
I am trying to implement a behavior for my draggable items. The behavior is described here : My box contains multiple draggable items which are sorted on page load I can drag and item from that box on a drop area But if I drag it back to the box it should re-place at its original position based on its data-attribute I have no clue how to achieve this. I saw that sortable could possibly do this but I don't know how to combine it with draggable. Thank you HTML
Item 1
Item 2
Item 3
Drop 1
Drop 2
Drop 3
JS $( ".box" ).draggable({ scope: 'demoBox', revert: true, cursorAt: { top: 40, left: 40 }, revertDuration: 100, start: function( event, ui ) { //Reset $( ".box" ).draggable( "option", "revert", true ); console.log('-'); } }); $( ".multiple-drag-area" ).droppable({ scope: 'demoBox', drop: function( event, ui ) { var area = $(this).find(".area").html(); var box = $(ui.draggable).html() $( ".box" ).draggable( "option", "revert", false ); //Display action in text console.log("[Action] " + box + "" + " dropped on " + "" + area + ""); //Realign item $(ui.draggable).detach().css({top: 0,left: 0, marginRight:4}).appendTo(this); }, }) $( ".drag-area" ).droppable({ scope: 'demoBox', drop: function( event, ui ) { var area = $(this).find(".area").html(); var box = $(ui.draggable).html() $( ".box" ).draggable( "option", "revert", false ); //Display action in text console.log("[Action] " + box + "" + " dropped on " + "" + area + ""); //Realign item $(ui.draggable).detach().css({top: 0,left: 0}).appendTo(this); }, accept: function(draggable) { return $(this).find("*").length-1 == 0 && (!$(this).hasClass("blocked-seat")); } }) JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Something like that should work: $( ".box" ).draggable({ revert: true, }); $( ".drag-area" ).droppable({ drop: function( event, ui ) { $(ui.draggable).detach().css({top: 0,left: 0}).appendTo(this); } }); $( ".multiple-drag-area" ).droppable({ drop: function( event, ui ) { let box = $(ui.draggable); // check if Element gets reinserted // we can check that by confirming the the box parent has the 'drag-area' class if($(box).parent().hasClass("drag-area")){ $(box).detach().css({top: 0,left: 0}).appendTo(this); // sort the boxes based on 'data-position' $(this).find('.box').sort(function( a, b ) { return a.dataset.position - b.dataset.position; }).appendTo(this); } } });

Related questions

0 votes
    I have a Post model that has a likes array that contains the ObjectId of the user that liked the ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 2, 2022 in Education by JackTerrance
0 votes
    I want to disable the sort property in dataTable API. I use bsort:false to disable sorting from all ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
0 votes
    I am new to jquery and now trying to create a fancy box in jquery . I will explain its ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 19, 2022 in Education by JackTerrance
0 votes
    I am new to jquery and now trying to create a fancy box in jquery . I will explain its ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 19, 2022 in Education by JackTerrance
0 votes
    I am new to jquery and now trying to create a fancy box in jquery . I will explain its ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 18, 2022 in Education by JackTerrance
0 votes
    I have the following Javascript code which controls an accordion type set of divs. The set of divs is ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 26, 2022 in Education by JackTerrance
0 votes
    Which of the following attribute is used to group items? A - item B - itemprop C - itemcheck D - itemgroup...
asked Dec 3, 2020 in Technology by JackTerrance
0 votes
    I have to use Draggable Slider component in my application. I have found one Link but it is not ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    Hi i'm using this to set a title attribute on elements: $('.upgradeTables thead tr td:gt(1)'). ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 2, 2022 in Education by JackTerrance
0 votes
    I have a simple MVC3 Controller Action like this [HttpGet] [OutputCache(Duration = 1200,Location=System.Web. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    Once the lower-level lock is released, the operation cannot be undone by using the old values of updated ... and Undo Operations in division Recovery System of Database Management...
asked Oct 10, 2021 in Education by JackTerrance
0 votes
    How to sort a data.table using vector of multiple columns in R? Select the correct answer from above options...
asked Feb 1, 2022 in Education by JackTerrance
0 votes
    How can I sort an event by id in DESC order? My all events are coming and by default they are ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 26, 2022 in Education by JackTerrance
0 votes
    How can I sort an event by id in DESC order? My all events are coming and by default they are ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
0 votes
    HOW DO I SORT THIS BY BIRTHYEAR IN ASCENDING ORDER? I'm distance learning student and have no help. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 5, 2022 in Education by JackTerrance
...