Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery change iteration direction

is it possible to iterate through elements from last to first one? For example from the last parent ul in a list to the nearest parent-ul one. At the moment I use each() but it would be nice to change direction...

Thanks!

like image 566
iop Avatar asked Mar 10 '26 17:03

iop


1 Answers

You can use the reverse() method on Arrays

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/reverse

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

or create your own function:

jQuery.fn.reverse = function() {
    return this.pushStack(this.get().reverse());
};

$("ul li").reverse().each(function(){ });

working example: http://jsfiddle.net/hunter/kdREt/

like image 111
hunter Avatar answered Mar 13 '26 05:03

hunter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!