jQuery nextOf/prevOf plugin

I’m hardly a web 2.0 guru, but the following jQuery tidbit makes it easy to move forward/backward in the DOM. I use it like $('.input').nextOf('.input').focus().


(function($){
$.fn.prevOf = function(selector, options) {
var defaults = { };
var options = $.extend(defaults, options);

var t = $(selector);
var i = t.index($(this).eq(0));
if (i > -1)
return t.eq(i-1);
return this;
}

})(jQuery);

(function($){
$.fn.nextOf = function(selector, options) {
var defaults = { };
var options = $.extend(defaults, options);

var t = $(selector);
var i = t.index($(this).eq(0));
return t.eq(i+1);
}

})(jQuery);

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s