Search code examples
jqueryjquery-uidraggablejquery-ui-draggable

drag and drop event fires twice


I am working on a script where i am having an issue while drag and drop list items.When i drag and item into a drop able area then event fires twice. here is my code.SEE CODE HERE

$('.draglist div').draggable({        
    cursor: 'move',
    helper: 'clone',
    connectToSortable: '.droplist'
}); 
$(".droplist").droppable({
drop: function (event, ui) {
  var dragtext = $(ui.draggable).text();
  alert(dragtext);
}
});
$('.droplist').sortable({
     opacity: 0.6,
     revert: true,
     cursor: 'move',
     placeholder: "highlight",
 });

See DEMO


Solution

  • This is a jQuery UI bug. A simple work-around is to use the use the sortable receive event rather than the droppable drop event. In doing so, the event is only fired once.

    Updated Example

    $(".droplist").droppable().sortable({
      opacity: 0.6,
      revert: true,
      cursor: 'move',
      placeholder: "highlight",
      receive: function(event, ui) {
        var dragtext = $(ui.item[0]).text();
        alert(dragtext);
      }
    });