0

I am trying to use the jquery-ui draggable to make some element draggable. I set the helper option to clone the current element.
It's making the clone correctly but when I drop the clone disappears. It doesn't stay at the dragged place.

See this for Demo Fiddle Link

$('#drag').draggable({
helper: function (e, ui) {
    return $(this).clone();
}
});

What am I missing ?

tezkerek
  • 186
  • 3
  • 6
Saif
  • 6,804
  • 8
  • 40
  • 61

2 Answers2

7

There maybe a simpler way, but through data of draggable, you can target a property that deals with this. Like this:

stop : function(e, ui){
         $('#drag').draggable().data()["ui-draggable"].cancelHelperRemoval = true;
    }

fiddle: http://jsfiddle.net/n10ucrLd/

Julien Grégoire
  • 16,864
  • 4
  • 32
  • 57
  • thanks for your response. There is another case with your fiddle. the newly created div are not move able. can we do anything about that.? – Saif May 26 '15 at 17:27
  • 1
    do you want them draggable with cloning as well? if not, you can simply apply draggable to cloning in your helper function. Like this: return $(this).clone().draggable(); – Julien Grégoire May 26 '15 at 17:30
3

I think there's been a lot of troubles with helper: 'clone'. I always got it to work, when I defined a droppable as well. E.g.:

HTML:

<div id="drag">Drag This</div>
<div class="container"></div>

JavScript:

$('#drag').draggable({
helper: function (e, ui) {
    return $(this).clone(true);
}
});

 $( ".container" ).droppable({
    drop: function (event, ui) {
       ui.draggable.clone().appendTo($(this)).draggable();
    }
 });

Live example: http://jsbin.com/vibeqaganu/1/edit?html,css,js,output

uglycode
  • 3,022
  • 6
  • 29
  • 55
  • thanks for your response. Can you please provide another link with your code? this link seems not working. – Saif May 25 '15 at 04:15