5

The API of JQuery Sortable isn't clear on how to retrieve the number of elements in a sortable list. The reason I want the number of elements, is because I want to set the position of a widget to the end of the sortable list. I know there is an append function, but I have my own logic to manipulate the sortable list.

Does someone know how to do this?

John Hendrik
  • 661
  • 1
  • 7
  • 20

3 Answers3

6

This should do it:

$('#sortable li').length;

since $(foo) returns either a jQuery object or an array of jQuery objects, depending on the selector foo, you can use the .length attribute.

Taryn
  • 242,637
  • 56
  • 362
  • 405
Zim84
  • 3,404
  • 2
  • 35
  • 40
3

you could use toArray method and check the length of the result array

Dziad Borowy
  • 12,368
  • 4
  • 41
  • 53
  • I like this method because my sortable is using divs, not li elements. I could use a version ofZim84's answer above with $("#sortable div").length, but why not let the sortable function keep track of its own child length. So -- $("#" + divID).sortable("toArray").length works for me. – jomofrodo Sep 15 '14 at 23:46
2

I think this might not be implemented in the jQuery sortable plugin (I haven't checked though), because just standard jQuery can count your list items.

for example:

var count = $("#myList li).length

gets you the number of records in the list <ul id="myList">

JDM
  • 931
  • 7
  • 9