I have an ul
called .products
and inside of that are a bunch of li
's called .product
.
Each li
has a data-id
on them with the product's id in the database.
I'm building the functionality so that when a user clicks "move up" or "move down", that product will be moved up or down one slot in the list.
$('.move-up').click(function(event) {
event.preventDefault();
var shownFieldsArray = $.makeArray($('.products'));
var currentIndex = $(shownFieldsArray).index($(event.currentTarget).parent());
shownFieldsArray.move(currentIndex, (currentIndex - 1));
// now I need to do something with the reordered shownFieldsArray
});
Move
is a function (link to another SO answer with the function) on the Array prototype that takes an old value and a new value and moves things accordingly.
So my question is: How should I replace the value of $('.products')
with the new, re-ordered list so the user can get visual confirmation? Should I remove the items and re-append them or is there something better like a replacement of .val()
?