Skip to main content

Bug Tracker

Side navigation

#2164 closed feature (fixed)

Opened January 15, 2008 11:33AM UTC

Closed September 09, 2009 12:21AM UTC

Last modified March 14, 2012 06:55AM UTC

.first() and .last() methods?

Reported by: simonw Owned by:
Priority: minor Milestone: 1.4
Component: core Version: 1.3.2
Keywords: Cc: simon+jquery@simonwillison.net
Blocked by: Blocking:
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 (0)
Change History (4)

Changed March 15, 2008 06:10AM UTC by flesler comment:1

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 March 29, 2008 10:05AM UTC by joern comment:2

resolution: → invalid
status: newclosed

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

Changed September 09, 2009 12:21AM UTC by john comment:3

milestone: 1.2.31.3.3
resolution: invalid
status: closedreopened
version: 1.2.21.3.2

Changed September 09, 2009 12:21AM UTC by john comment:4

resolution: → fixed
status: reopenedclosed

Landed in SVN rev [6541].