简体   繁体   中英

How to find index of div which is dropped on droppable div

I have drag and drop functionality in which I drag div and dynamically drop on droppable area. I have sortable functionality in droppable area, So I want to find out indexing so that after sorting I have to find out exact index position of a particular div. here is my code

$('#workArea').sortable({cancel: ':input,button,.common'});
$(".drags").draggable({
  helper : "clone",
  connectWith: '.workArea'
});
$(".workArea").droppable({
  drop: function( event, ui ) {
    function createHead(workspace){
      var head = document.createElement("div");
    }
  // [editor] i think i am missing a } here or pasted something wrong
});

jQuery provides the .index() method,

the return value is an integer indicating the position of the first element within the jQuery object relative to its sibling elements.

Read more about .index()

It seems to me that the best place to return your new index will be via your sorting function.

Updating the index in a global array might help too. Then search for your specific div element in array after you have called your sort function.

In JS you can always use this.target is the element you are dropping and then using
$(this.target).index() and finding it with $("#parentContainer div:eq(" + newIndex + ")")

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM