0

I have a list of draggable items that can be dragged into a sortable and rearranged. I have that bit working ok.

What I am now trying to do is to add a delete link to each item once it has been dropped. The code I currently have fires twice so I get to delete links for each item and I can't figure out why.

Here's the code:

    $(function () {
    var order = null;

    $("#sortable")
        .sortable({
            revert: true,
            placeholder: "ui-state-highlight"
        })
        .droppable({
            drop: function (event, ui) {
                addControls(ui.draggable);
            }
        });

    $(".draggable").draggable({
        connectToSortable: "#sortable",
        helper: "clone",
        revert: "invalid"
    });
    $("ul, li").disableSelection();
});

function addControls($item) {
    $item.append('<a href="#">delete</a>');
}

There's a JS fiddle here to play with: http://jsfiddle.net/2X7zk/

jhob101
  • 432
  • 2
  • 6
  • 15
  • possible duplicate of [Drop() gets called twice with Sortable/Droppable](http://stackoverflow.com/questions/10914503/drop-gets-called-twice-with-sortable-droppable) – A. Wolff Jun 14 '13 at 09:17

1 Answers1

0

As a simple workaround:

drop: function (event, ui) {
              if (!ui.draggable.find('.delete').length) addControls(ui.draggable);
          }

function addControls:

function addControls($item) {
      $item.append('<a href="#" class="delete">delete</a>');
  }

DEMO

A. Wolff
  • 74,033
  • 9
  • 94
  • 155