Search code examples
jquery-uijquery-ui-draggablejquery-ui-droppable

Prevent droppable from accepting when it is overlaid by another element in jqueryUI?


I've positioned 2 containers such a way that container one overlays on another. But when I drop an item onto the container one, the dropped item goes into both containers. How do I restrict that?

   //JS Code: 
   $("div.draggable").draggable({
      helper: "clone",
      cursor: "move",
      containment: 'body'
    });


    $("div.droppable").droppable({
      addClasses: false,
      drop: function (event, ui) {
        var $draggable = $(ui.draggable),
            $droppable = $(this);

        $droppable.html($draggable.clone());
      }
    });

Demo: http://jsfiddle.net/HL8VR/


Solution

  • Thanks @Nal but nothing works for me, So I Created this hack.

    I store some data with an element once an item is dropped onto it. Unfortunately the data is stored with both Droppables which I could not isolate.

    $("div.droppable").droppable({
      addClasses: false,
      drop: function (event, ui) {
        var $draggable = $(ui.draggable),
            $droppable = $(this);
    
        $droppable.data({'drop': true, 'draggable': $draggable});
      }
    });
    

    But once the item is dropped, I could figure out which droppable I'm hovering on.

    $('div.droppable').hover(function() {
        if ($(this).data('drop') === true) {
            $(this).html($($(this).data('draggable')).clone());
    
            // Clears the data from both droppables to avoid duplicating the item in them
            $('div.droppable').data({'drop': false, 'draggable': false});
        }        
    });
    

    Demo: http://jsfiddle.net/codef0rmer/AVQVs/