views:

20

answers:

1

Hi,

I want a user to be able to reorder a list on a click event. I have set up a very basic jsFiddle here:

http://jsfiddle.net/8vWXZ/2/

So in this example i would like to move the clicked li item and its contents to the position above in the DOM. I suppose strictly speaking it is being moved down in the index of li items.

This is going to be extended later to include buttons that move li items up and down and a submit button that saves the order/state of the list items.

I have found a lot of info on drag and drop (which is not required) but not much on this sort of functionality. If someone could point me in the right direction that would be great :)

+1  A: 

Something you might want to check out is the jQuery UI sortable

If you simply want to move items around by clicking etc. have a look at the following methods:

detach function

DOM insertion functions

$("document").ready(function() {
    $("#reOrder li").click(function() {
        var index = $("#reOrder li").index(this);
        if (index != 0) {
           // li item is not sitting in the first position
           $("#reOrder li:eq(" + (index - 1) + ")").before(this);
        }
    });
});

Detach will allow you to remove the clicked element from the DOM and will have a copy available, you can then reinsert in the desired position using one of the DOM insertion functions.

xiaohouzi79
That's great. Have updated it now but would like to unbind the click event on the first li to stop them being removed when i click the first item.
RyanP13
Hi Ryan, have a look at the example function (above), it checks the item at index 0 to make sure it doesn't try calling before() on that item.
xiaohouzi79
Updated here:http://jsfiddle.net/8vWXZ/6/Thanks.
RyanP13