Bug Tracker

Opened 8 years ago

Closed 8 years ago

Last modified 7 years ago

#7411 closed feature (wontfix)

Add reverse() function to jQuery core

Reported by: jez9999@… 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;

Change History (8)

comment:1 Changed 8 years ago by jez9999@…

Forgot to mention; typical use case for this function:

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

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

});

comment:2 Changed 8 years ago by Rick Waldron

Cc: addyosmani snover john dmethvin slexaxton ajpiano jitter added
Component: unfiledcore
Keywords: needsreview added
Priority: undecidedlow
Status: newopen

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

comment:3 Changed 8 years ago by addyosmani

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

comment:4 Changed 8 years ago by ajpiano

+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.

comment:5 Changed 8 years ago by dmethvin

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.

comment:6 Changed 8 years ago by snover

Cc: snover removed

comment:7 Changed 8 years ago by Rick Waldron

Owner: set to ajpiano
Status: openassigned

comment:8 Changed 8 years ago by jitter

Cc: jitter removed
Keywords: needsreview removed
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
Note: See TracTickets for help on using tickets.