Skip to main content

Bug Tracker

Side navigation

#7411 closed feature (wontfix)

Opened November 05, 2010 10:17AM UTC

Closed January 29, 2011 04:58AM UTC

Last modified March 15, 2012 12:54PM UTC

Add reverse() function to jQuery core

Reported by: jez9999@gmail.com Owned by: ajpiano
Priority: low Milestone: 1.5
Component: core Version: 1.4.4rc
Keywords: Cc: addyosmani, john, dmethvin, slexaxton, ajpiano
Blocked by: Blocking:
Description

Why isn't there a reverse() function in the jQuery core, to reverse the order of objects in a jQuery collection? This is a very simple thing to implement, yet I feel it should be in the core so that people can quickly get the functionality instead of having to hunt it down on the web and define their own function. Something like this should do:

jQuery.fn.reverse = Array.prototype.reverse;

Attachments (0)
Change History (8)

Changed November 05, 2010 10:22AM UTC by jez9999@gmail.com comment:1

Forgot to mention; typical use case for this function:

jQuery('div.stuffToReverse').children().reverse().each(function(){

alert('contents of child: ' + jQuery(this).text());

});

Changed November 05, 2010 01:19PM UTC by rwaldron comment:2

cc: → addyosmani, snover, john, dmethvin, slexaxton, ajpiano, jitter
component: unfiledcore
keywords: → needsreview
priority: undecidedlow
status: newopen

I think this might indeed be useful, lets see what the others have to say

Changed November 05, 2010 03:37PM UTC by addyosmani comment:3

I second rwaldron's comments. This looks like it should belong in core and has been requested outside the tracker a number of times over the past few years.

I see that Brandon touched upon this too last year http://forum.jquery.com/topic/jquery-reverse-a-collection-of-jquery-elements

Changed November 05, 2010 03:48PM UTC by ajpiano comment:4

+1 on this. It's barely any code to implement, and I've had to provide it to a lot of people who have asked over time.

Changed November 06, 2010 02:48PM UTC by dmethvin comment:5

The ticket seems to have 0 votes at the moment ... :)

Most jQuery operations return nodes in document order, so we'd need to document what happens if other operations are performed after a reverse.

Changed November 08, 2010 05:29PM UTC by snover comment:6

cc: addyosmani, snover, john, dmethvin, slexaxton, ajpiano, jitteraddyosmani, john, dmethvin, slexaxton, ajpiano, jitter

Changed November 10, 2010 08:41PM UTC by rwaldron comment:7

owner: → ajpiano
status: openassigned

Changed January 29, 2011 04:58AM UTC by jitter comment:8

cc: addyosmani, john, dmethvin, slexaxton, ajpiano, jitteraddyosmani, john, dmethvin, slexaxton, ajpiano
keywords: needsreview
resolution: → wontfix
status: assignedclosed

After some discussion dmethvin, ajpiano and me came to the conclusion that adding a reverse function to the core wouldn't be of great use. Especially if you consider chaining and that most other methods would immediately restore the dom-order of the elements.

If you just need the current elements in the set in reverse order just do

//get a plain array of elements then call reverse on that
$(something).get().reverse();

If you insist having this as a method on the jQuery object you can monkey patch it using something along these lines

$.fn.reverse = Array.prototype.reverse