Ok, I've gone ever-so-slightly farther with this one. It works basically the same as your last post (which was very well done btw!), but also passes the value of the predicate into the callback (if the predicate is truthy, of course) so that the callback can utilize it. Sure, it's a total edge-case, but hey, you never know! If this were a stand-alone plugin (which, it might just become) I'd add in a var reference to arguments and undefined to aid in minification.
jQuery.fn.extend({ cond: function() { var i = 0, predicate, callback, result; while ( !predicate && i < arguments.length ) { predicate = arguments[ i++ ]; callback = arguments[ i++ ]; predicate = jQuery.isFunction( predicate ) ? predicate.call ( this ) : predicate; result = !callback ? predicate : predicate ? callback.call( this, predicate ) : undefined; } return result !== undefined ? result : this; } }); var x = 1; function test() { return x === 3 ? 'aqua' : x === 4 ? 'fuchsia' : false; } jQuery('a') .cond( x === 1, function(){ this.css({ color: 'blue' }); }, x === 2, function(){ this.css({ color: 'red' }); }, test, function( val ){ this.css({ color: val }); }, function(){ this.css({ color: 'green' }); } ).length; On Jun 10, 6:40 pm, "stephb...@googlemail.com" <stephb...@googlemail.com> wrote: > Here's a version (with call!) that allows you to > return whatever you like to the chain. If you return > undefined, it returns the original jQuery object unmodified. > > jQuery.fn.extend({ > condition: function() { > var i, test, result; > for (i=0; i<arguments.length; i=i+2) { > test = typeof(arguments[i])==='function' ? arguments > [i].call(this) : arguments[i] ; > > if (!arguments[i+1]) { > result = test; > break; > } > > if (test) { > result = arguments[i+1].call(this); > break; > } > } > return result === undefined ? this : result; > } > > > > }); --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "jQuery Development" group. To post to this group, send email to jquery-dev@googlegroups.com To unsubscribe from this group, send email to jquery-dev+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/jquery-dev?hl=en -~----------~----~----~----~------~----~------~--~---