jquery / jquery/jquery-ui

Clone draggable and make the cloned element draggable as well

Open
#2,162 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Behavior shared with 1.12 Comp: Draggable
Dominant language
JavaScript
Stars
11.3k
Forks
5.2k
PR merge metrics
No merged PRs in 30d

Description

I want to be able to clone the main draggable elements and be able to drag the cloned element as well.

You can run the code here: https://jsfiddle.net/2qatpe47/

CSS:

.header{
  padding: 20px;
  background: #000;
  color: #fff;
  text-align: center;
}

.drag{
  background: red;
  padding: 10px;
  margin-right: 20px;
}

.zone{
  width: 80%;
  margin: 40px auto;
}

.drop{
  display: inline-block;
  width: 27%;
  border: 1px solid #000;
  height: 70px;
  padding: 10px;
  vertical-align: middle;
  text-align: center;
}

.ui-droppable-disabled {
    background: #494f54;
}

HTML:

<div class="header">
  <span class="drag main">X</span>
  <span class="drag main">O</span>
</div>
<div class="zone">
  <div class="drop"></div>
  <div class="drop"></div>
  <div class="drop"></div>
  <div class="drop disabled"></div> <!-- Droppable Disabled -->
  <div class="drop"></div>
  <div class="drop"></div>
  <div class="drop"></div>
  <div class="drop"></div>
  <div class="drop"></div>
</div>

jQuery:

$(".drag").draggable({
    helper: "clone"
});


$(".drop").droppable({
    accept: ".drag",
    drop: function(event, ui) {
        //If the dragged element is `X` or `O` on the top
        if($(ui.draggable).hasClass('main')){
            var cloned = $(ui.draggable).clone();
          
            cloned.removeClass('main');

            $(this).append(cloned);

            //cloned.draggable();
        }
        
        $(this).droppable('disable');
    },
    out: function(event, ui) {
        $(this).droppable('enable');
    }
});

//Disable Droppable for elements with .disabled
$('.disabled').droppable('disable');

The issue is that I can't drag the cloned element after dropping it. I tried to add this cloned.draggable(); it's commented out to make that cloned element draggable but the issue is that I can drag it to disabled and to other cells which already contain cloned elements and the cell that element dragged out is disabled.

I want each cell to contain only one dragged element and if I move the element to another cell it shouldn't be disabled.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Run the linked JSFiddle and begin with the draggable and droppable setup shown in the issue. Trace the clone created in the drop callback and the disabled-cell behavior; done means cloned elements remain draggable, occupied or disabled cells reject them, and moving an element re-enables its former cell.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, jquery
Domain
frontend
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.