A jQuery function that performs an .each in reverse

Sometimes you need to start at the end of a set of selected items. The .each function runs a function starting from the beginning, but theres nothing similar to loop backwards. The reverse selector function does just that.

jQuery.fn.reverse = function(fn) {

   var i = this.length;

   while(i) {
        i--;
        fn.call(this[i], i, this[i])
   }
};

Usage:

$('#product-panel > div').reverse(function(i, e) {
    alert(i);
    alert(e);
});
A jQuery function that performs an .each in reverse

Leave a Reply

Your email address will not be published. Required fields are marked *