1

I am trying to get a droppable area to accept multiple divs but it will only accept the most recently declared. I'm having trouble finding out how to go about this problem.

Here is the code:

$(".square").draggable({helper: 'clone'});
$(".rect").draggable({helper: 'clone'});

$("#canvas").droppable({
    accept: ".square",
    accept: ".rect",
    drop: function(ev,ui){
        $(ui.draggable).clone.appendTo(this);
user2680746
  • 11
  • 1
  • 2

3 Answers3

5

When you pass the accept option twice to a object the latest one will overwrite the first one

Try

$(".square").draggable({helper: 'clone'});
$(".rect").draggable({helper: 'clone'});

$("#canvas").droppable({
    accept: ".rect, .square",
    drop: function(ev,ui){
        $(ui.draggable).clone().appendTo(this);
    }
});

Demo: Fiddle

Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
  • may I ask you to take a look at a jQuery draggable and droppable related question here : https://stackoverflow.com/questions/54498364/jquery-drag-and-drop-simulation-does-not-work-for-the-last-draggable ? – Istiaque Ahmed Feb 03 '19 at 19:17
0

You should use "accept:" only once to define the selector of accepted elements

Michael B.
  • 2,798
  • 1
  • 14
  • 18
0

You should try making a separate class from square and rect, such as "droppableShape". Then add it as a second class to each of them and just use one accept.

HTML

<...class="square droppableShape"..../>
<...class="rect droppableShape".../>

JQuery

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

$("#canvas").droppable({
accept: ".droppableShape",
drop: function(ev,ui){
    $(ui.draggable).clone.appendTo(this);
Tricky12
  • 6,752
  • 1
  • 27
  • 35