I’ve got a list of forum topics I update every 20 seconds checking if there are new posts. If there is, I take that topic and move it to the top of the list, like this
$('#topicID').insertBefore("#topics li:first");
This does work pretty well, unfortunately list items disappear if they are already at the top. Same holds true for before(). Is this considered a bug or intended behavior?
I realise it doesn’t make much sense to insert an element before itself and that this can be solved easily by adding a .remove()-Statement before the insert, like this:
$('#topicID').remove().insertBefore("#topics li:first");
Nevertheless I think it would make more sense for jQuery to insert the element at the point it originally was than to just drop it. What are your thoughts on this?
Either way, this post might help some people figure out why there might be stuff missing from their html.
Here’s the code showing what I mean:
http://jsfiddle.net/93UqQ/2/
Try using
.prependToinstead:This will move the element without removing it from the DOM, and if it’s already in the correct place, nothing will change.
http://jsfiddle.net/mblase75/93UqQ/5/