JQuery .each() backwards

Jack Mills picture Jack Mills · Sep 8, 2009 · Viewed 206.7k times · Source

I'm using JQuery to select some elements on a page and then move them around in the DOM. The problem I'm having is I need to select all the elements in the reverse order that JQuery naturally wants to select them. For example:

<ul>
   <li>Item 1</li>
   <li>Item 2</li>
   <li>Item 3</li>
   <li>Item 4</li>
   <li>Item 5</li>
</ul>

I want to select all the li items and use the .each() command on them but I want to start with Item 5, then Item 4 etc. Is this possible?

Answer

Joe Chung picture Joe Chung · Sep 8, 2009
$($("li").get().reverse()).each(function() { /* ... */ });