Bug Tracker

Modify

Ticket #7411 (closed feature: wontfix)

Opened 3 years ago

Last modified 15 months ago

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
Blocking: Blocked by:

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

comment:1 Changed 3 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 3 years ago by rwaldron

  • Cc addyosmani, snover, john, dmethvin, slexaxton, ajpiano, jitter added
  • Keywords needsreview added
  • Status changed from new to open
  • Component changed from unfiled to core
  • Priority changed from undecided to low

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

comment:3 Changed 3 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 3 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 3 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 3 years ago by snover

  • Cc snover removed

comment:7 Changed 3 years ago by rwaldron

  • Owner set to ajpiano
  • Status changed from open to assigned

comment:8 Changed 2 years ago by jitter

  • Cc jitter removed
  • Keywords needsreview removed
  • Status changed from assigned to closed
  • Resolution set to wontfix

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

Please follow the  bug reporting guidlines and use  jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

View

Add a comment

Modify Ticket

Action
as closed
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.