0

I am using jquery ui draggable and am allowing the user to change the order of a list, when they are done I plan to have them click a button and I will create a json file with their current list order, what is the best way to do something like this using jquery since the list order is not written on html file?

ps: each li tag and ul tag may have an id.

scottheckel
  • 9,106
  • 1
  • 35
  • 47
ittechie
  • 61
  • 6
  • 4
    I'm pretty sure that when you drag a jQuery UI [sortable](http://jqueryui.com/demos/sortable/) item, the DOM is in fact altered and the `li` elements will be in the order you see them. http://jqueryui.com/demos/draggable/#sortable – Blazemonger May 08 '12 at 13:43
  • 1
    http://stackoverflow.com/a/4856309/29995 – Rob Hruska May 08 '12 at 13:45

2 Answers2

1

You can use

jQuery(myListItemSelector).each(function(index, item){
    // Do something here...
})

to loop through the items extracting the ID, attribute or content into your JSON list.

Kim R
  • 561
  • 2
  • 13
0

Expanding on Kim's answer. If you are not guaranteed each item has an id and since you are using jQuery. You should use the jQuery.data() or jQuery data() to attach context to the items so you know which one is which. It will look something like this if that context is just the original order.

jQuery(myListItemSelector).each(function(index, item){
    // Do something here...
    jQuery(this).data("index", index);
});

Then when you are saving it you can loop through again and pull that index value out. If there is some other defining value that would make these unique it would be easier for you than an index. Something like a GUID really comes in handy in situations like this.

scottheckel
  • 9,106
  • 1
  • 35
  • 47