Very useful. One thought: It might be nice to allow the elseCallback to run
even when ifCallback is not provided.

George


J?rn Zaefferer wrote:
> 
> Hi folks,
> 
> the thread about showing and hiding DDs when clicking DTs gave me an 
> idea for a chainable if/else construct. I have implemented and tested it 
> and now I'd like to know if this is useful enough to be released as a 
> plugin or even adding to the core.
> 
> Basically I extended the current is(String expression) method. It now 
> accepts one or two functions. If one is given, it will be executed for 
> every element that fits the expression. If the second function is given, 
> it will be executed for every element that does not fit the expression.
> 
> Applied to the click dt to show/hide dd example:
> $(document).ready(function() {
>     $('#faq').find('dd').hide().end().find('dt').click(function() {
>         $(this).next().is(':visible',
>             function() { $(this).slideUp(); },
>             function() { $(this).slideDown(); }
>         );
>     });
> });
> 
> The extended is method looks like this:
>     is: function(expr, ifCallback, elseCallback) {
>         if(ifCallback) {
>             var elseCalllback = elseCallback || function() {};
>             return this.each(function() {
>                 if($(this).is(expr)) {
>                     ifCallback.apply(this);
>                 } else {
>                     elseCallback.apply(this);
>                 }
>             });
>         }
>         return expr ? jQuery.filter(expr,this).r.length > 0 : false;
>     },
> 
> The strength of this addition: is() returns the jQuery object, therefore 
> it does not break the chain. Your opinions?
> 
> -- J?rn
> 
> 
> _______________________________________________
> jQuery mailing list
> discuss@jquery.com
> http://jquery.com/discuss/
> 
> 

-- 
View this message in context: 
http://www.nabble.com/Chainable-if-else---hot-or-not--tf2363128.html#a6592731
Sent from the JQuery mailing list archive at Nabble.com.


_______________________________________________
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/

Reply via email to