Possible Duplicate:
JQuery .each() backwards
What is the easiest way to use .each()
in reverse? At the moment I am doing this:
var temp = [];
$("#nav a").each(function()
{
temp.push($(this));
});
temp.reverse();
for(var i = 0; i < temp.length; i++)
{
var a = temp[i];
// Work with a.
}
It would be nice if I could do something like:
$("#nav a").reverse().each(function()
{
// Work with $(this).
});
The context is that I have a collection of elements using float: right
which displays them in reverse order and I want to iterate over them from left to right like normal.
Use this
$($("#nav a").get().reverse()).each(function() {
//..........
});
I can't take credit for this because I saw it somewhere else here on SO but I think this should help; add the functionality to jQuery's prototype like so:
$.fn.reverse = [].reverse
Then use it as indicated above:
$('#nav a').reverse(); // returns a reversed array of elements
Hope this helps :)