Sunday, 29 September 2013

Removing and prepending in jQuery

Removing and prepending in jQuery

I am trying to achieve a simple unordered list, with the following
behaviour. When a list item is clicked, it needs to go on top of the list.
I managed to do it by assigning the element to a variable, removing "this"
(the clicked item) and prepending my element variable.
This seems to work. Only once.
I guess this is because the elements are removed from the DOM, and the new
ones are not "detected". WHat is the right way to do it?
Here's my simple code:
$('ul.selekta li').on('click', function() {
var element = $(this);
$(this).remove();
$('ul.selekta').prepend(element);
});
Any help is super appreciated!

No comments:

Post a Comment