Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get ID of dragged list item in a sortable jQuery

I have this html:

<ul>
    <li id='item1'>First</li>
    <li id='item2'>Second</li>
    <li id='item3'>Third</li>
</ul>

and this .sortable jQuery:

$(function(){
    $("#listofpages").sortable({

    }
})

How can I get the id of the dragged element?

like image 536
kganciro Avatar asked Dec 04 '13 04:12

kganciro


2 Answers

Inside the update event callback you can do this (demo):

$( "#listofpages" ).sortable({
  update: function( event, ui ) {
    var id = ui.item.attr("id");
  }
});
like image 125
Jason Sperske Avatar answered Sep 23 '22 20:09

Jason Sperske


The previous answer was very good however you should use the receive event not update, update fires twice as often as needed and can cause problems because it is firing once for element removed from previous list and fired once for element added to new list.

$( "#listofpages" ).sortable({
  receive: function( event, ui ) {
    var id = ui.item.attr("id");
  }
});
like image 23
Nick Hanshaw Avatar answered Sep 24 '22 20:09

Nick Hanshaw