> From: Dossy Shiobara > > I'm surprised there's no .reverse(). i.e.: > > $(collection).reverse().each(...)
Great idea! How about the world's smallest plugin: jQuery.fn.reverse = [].reverse; Try it out at http://jquery.com/ by entering these lines into the FireBug console: jQuery.fn.reverse = [].reverse; $('h3').each( function() { console.info( this.innerHTML ); } ); $('h3').reverse().each( function() { console.info( this.innerHTML ); } ); Just for fun, you can add .sort(): jQuery.fn.sort = [].sort; And then try this: $('h3').sort( function( a, b ) { var a = a.innerHTML, b = b.innerHTML; return a > b ? 1 : a < b ? -1 : 0 }).each( function() { console.info( this.innerHTML ); } ); Or all on one line for the FireBug console: $('h3').sort( function( a, b ) { var a = a.innerHTML, b = b.innerHTML; return a > b ? 1 : a < b ? -1 : 0 } ).each( function() { console.info( this.innerHTML ); } ); It looks like you can add pretty much any Array method this way, although most of the others duplicate what you can do in other ways. For example: jQuery.fn.shift = [].shift; And then try: $h3 = $('h3') $h3 $h3.shift() $h3 $h3.shift() $h3 Armed with this knowledge, one might be tempted to load all the Array methods in one fell swoop: jQuery.fn.prototype = Array.prototype; But that does not work, presumably because jQuery is already being a bit sneaky about its Array-like behavior. -Mike _______________________________________________ jQuery mailing list discuss@jquery.com http://jquery.com/discuss/