If I have the following html:
- test
- special
- test
<
To save an object's position, you can just save the DOM reference to the sibling before it. If there is no sibling before it, then save the parent.
function saveLocation(element) {
var loc = {};
var item = $(element).prev();
loc.element = element;
if (item.length) {
loc.prev = item[0];
} else {
loc.parent = $(element).parent()[0];
}
return(loc);
}
Then, to restore:
function restoreLocation(loc) {
if (loc.parent) {
$(loc.parent).prepend(loc.element);
} else {
$(loc.prev).after(loc.element);
}
}