JQuery .each() backwards

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?

11 Answers
11

$($("li").get().reverse()).each(function() { /* ... */ });

Leave a Comment