Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the order position JQuery sortable list?

Tags:

jquery

How can i get the order position of Jquery sortable list?

like image 339
pegasus Avatar asked Nov 12 '10 10:11

pegasus


1 Answers

If your elements have IDs (if they don't, pick a prefix and number them) you can use the toArray method to get the array of IDs in their current order, for example:

var idsInOrder = $(selector).sortable("toArray");

If you want to find the position of a particular one, use $.inArray(), like this:

var index = $.inArray("idToLookFor", idsInOrder);

This will return the 0-based index in the list the ID you're looking for is, or -1 if it's not found.


More generally, if you just want to get the index of an element amongst its siblings, use .index() with no parameters, like this:

$("#myElem").index();

If your sortable was for example a <ul> with sortable <li> children, calling this on an <li> would also get it's 0-based index, within it's siblings.

like image 197
Nick Craver Avatar answered Sep 30 '22 20:09

Nick Craver