$.each({ prevOf: "previousSibling", nextOf: "nextSibling" }, function( method, traversal ) { $.fn[ method ] = function( selector ) { return this.pushStack( this.map(function() { var ret = this[ traversal ]; while ( ret && !$( ret ).is( selector ) ) { ret = ret[ traversal ]; } return ret; }) ); }; });