|
|
(jQuery) .nextOrFirst() function guarantees a selection
by Matt on 2010/06/28 at 1:26 pm
For instance, a function that is triggered at least once at some point, and then automatically executes itself again each time it finishes.
Prerequisites: jQuery must be loaded before using code snippet. Some experience with jQuery is necessary.
The .nextOrFirst() jQuery function/plugin detailed here works as an alias for .next(), except when there are no more objects for .next() to select. When this happens, .nextOrFirst() will automatically select the first sibling (essentially treating the first element as “next”). By itself, .next() is used, not surprisingly, to change any current selection to that selection’s next sibling (you can read more about it here). By default, if .next() is used on the last element within any given parent (for instance, the last <li> in a <ul>), it will return nothing. But what if you want to ensure that .next() always returns a selection? For instance, what if we want to get the first <li> if we reach the last one? At some point you will run into scripting situations where you’ll need this very functionality – particularly if scripting something that works on a timed Note: Thanks to Billyzkid for providing a much more efficient and concise version of these functions, which I’m now using for the default examples below.
jQuery: .nextOrFirst()
/********************************************************************************
* jQuery.nextOrFirst()
*
* PURPOSE: Works like next(), except gets the first item from siblings if there is no "next" sibling to get.
********************************************************************************/
jQuery.fn.nextOrFirst = function(selector){
var next = this.next(selector);
return (next.length) ? next : this.prevAll(selector).last();
}
Remember that in order to use this function, jQuery must already be loaded. If you need a prevOrLast() function, use this: jQuery: .prevOrLast()
/********************************************************************************
* jQuery.prevOrLast()
* PURPOSE:
* Works like prev(), except gets the last item from siblings if there is no "prev" sibling to get.
********************************************************************************/
jQuery.fn.prevOrLast = function(selector){
var prev = this.prev(selector);
return (prev.length) ? prev : this.nextAll(selector).last();
}
|
2 Comments on (jQuery) .nextOrFirst() function guarantees a selection
Nice idea, but I think these are more concise, faster (less jQuery object creation), and avoids a subtle bug. It looks your children selector will only check the first/last child of the parent before filtering, whereas these functions will always find the correct first/last match:
$.fn.nextOrFirst = function(selector)
{
var next = this.next(selector);
return (next.length) ? next : this.prevAll(selector).last();
};
$.fn.prevOrLast = function(selector)
{
var prev = this.prev(selector);
return (prev.length) ? prev : this.nextAll(selector).last();
};
That’s definitely a more efficient solution. Thanks for sharing!