manbearpig1 manbearpig1 - 2 months ago 7
HTML Question

Dynamically append new links to top of list instead of bottom of list

I am dynamically adding links to my list. The newest links are added to the bottom of the list. How would one append each new link to the top of the list?

<ul class="posts" id="posts">
<% for(var i=0; i<posts.length; i++) { %>
<li class="post">
<span><a href="/collab/<%=posts[i]['_id']%>"><%= posts[i].userCreate %></a></span>
</li>
<% } %>
</ul>

Answer

If I understand what you are asking correctly you need to iterate your list from the end to the front:

for (var i = posts.length - 1; i >= 0; i--) {