jQuery: The Write Less, Do More JavaScript Library

Ticket #2164 (closed feature: invalid)

Opened 4 months ago

Last modified 2 months ago

.first() and .last() methods?

Reported by: simonw Assigned to: anonymous
Type: feature Priority: minor
Milestone: 1.2.3 Component: core
Version: 1.2.2 Keywords:
Cc: simon+jquery@simonwillison.net Needs: Review

Description

Is there any reason jQuery doesn't include methods for getting the .first() and .last() items in the current collection? I frequently use the :first and :last CSS selectors, so a method equivalent would be useful (and intuitive: I just had someone ask about a bug in their jQuery code caused by their assumption that .first() was a valid method).

Implementation:

jQuery.fn.first = function() { return this.eq(0) };
jQuery.fn.last = function() { return this.eq(this.size() - 1) };

Attachments

Change History

Changed 2 months ago by flesler

this.eq(0) simply takes less bytes, why do you need another method. As for last(), you can do this.slice(-1), also pretty straightforward.

Changed 2 months ago by joern

  • status changed from new to closed
  • resolution set to invalid

Agree with Ariel. first/last were part of the API in earlier versions and not worth the effort.

Note: See TracTickets for help on using tickets.