Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery ui drop helper

Using jQuery and Jquery UI, I have a draggable and droppable area, the draggable item has the following helper

  $(".draggable").draggable({
    revert: 'invalid',
    grid: [ 20,20 ],
    cursorAt: { top: -12, left: -20 },
    helper: function(event) {
      return $('<div class="helper"></div>');
    }
  });

How do I get the helper to be added to the droppable area?

like image 960
Alan Whitelaw Avatar asked Jan 10 '11 22:01

Alan Whitelaw


People also ask

How does jQuery ui draggable work?

jQueryUI provides draggable() method to make any DOM element draggable. Once the element is draggable, you can move that element by clicking on it with the mouse and dragging it anywhere within the viewport.

Why is draggable not working?

Check whether your draggable object is already loaded in the viewport. If it is not, it won't work properly. JUST AFTER the draggable object to be absolutely sure that everything is loaded at the correct time. When you'll be sure everything is ok, then you'll be able to refactor.

How do you drag and drop in jQuery?

Using jQuery UI, we can make the DOM(Document Object Model) elements to drag anywhere within the view port. This can be done by clicking on the draggable object by mouse and dragging it anywhere within the view port. If the value of this option is set to false, it will prevent the DOM elements to be dragged .


1 Answers

After a bit more investigation and another question I have worked this out.

The in the drop event on the droppable element you need to clone the helper as you cannot drop the actual helper that shows during dragging.

$("#droppable").droppable({
  drop: function(event, ui) {
    var newDiv = $(ui.helper).clone(false)
      .removeClass('ui-draggable-dragging')
      .css({position:'absolute', left:0, top:ui.offset.top - 12});
    $(this).append(newDiv);
  }
});

Thanks also to Jason Benson.

Alan

like image 185
Alan Whitelaw Avatar answered Oct 02 '22 17:10

Alan Whitelaw