This thread got me thinking, as chaining conditions is something I've found myself wanting to do occasionally when quickly writing jQuery. I'm not sure I'm sold on the idea of having the condition affect the current chain, though. I'd prefer something more like the event helper syntax. Here's my contribution:
jQuery.fn.extend({ condition: function() { for (var i=0; i<arguments.length; i=i+2) { if (arguments[i]) { this.condition = (arguments[i+1]) ? arguments[i+1]: arguments[i]; this.condition(); this.condition = jQuery.fn.condition; break; } } return this; } }); It allows you to write chainable jQuery conditions thus: jQuery("#element").condition(condition, fn) The arguments can contain any number of condition / function pairs that act as 'if' and 'else if'. If the final argument is a function with no preceding condition it acts as 'else'. Inside the function 'this' is the current jQuery collection: jQuery("#element") .condition( (x===1), function(){ this.css({color: 'blue'}); }), (x===2), function(){ this.css({color: 'red'}); }), function(){ this.css({color: 'green'}); }); I'm really not sure of the merits of temporarily overwriting the condition method to get 'this' to represent the current collection. That's probably not clever. Certainly it means you can't put one condition inside another. Does anyone know a better way, without having to look for an unused name in the jQuery.fn.x namespace? --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---