jQuery - monkeypatch function arg to most traversing methods
by mofle
HTML
<div id="a">a</div>
<div id="b">b</div>
<div id="c">c</div>
<div id="d">d</div>
JavaScript
// Monkeypatch support for a function argument on the following methods:
(function( $ ) {
$.each( "parent parents parentsUntil next prev nextAll prevAll nextUntil prevUntil siblings children contents".split(" "), function( i, val ) {
var _self = $.fn[ val ];
$.fn[ val ] = function( until ) {
until = $.isFunction( until ) ? until.call( this, until ) : until;
return _self.apply( this, arguments );
};
});
})( jQuery );
console.log( $('#a').nextUntil(function() {
return $(this).siblings('#d');
}) );
// Compressed - 261 chars
(function(b){b.each("parent parents parentsUntil next prev nextAll prevAll nextUntil prevUntil siblings children contents".split(" "),function(e,c){var d=b.fn[c];b.fn[c]=function(a){a=b.isFunction(a)?a.call(this,a):a;return d.apply(this,arguments)}})})(jQuery);