How about something like (although i admit the function name could
use some thinking, heh)
(function($) {
$.fn.showorhide = function(bool) {
if (bool) {
$(this).show();
}
else {
$(this).hide();
}
return this;
};
})(jQuery);
On Nov 18, 9:02 am, "Dylan Verheul" <
dylan.verh...@...> wrote:
> On Tue, Nov 18, 2008 at 14:56, Karl Swedberg <
k...@...> wrote:
> > It struck me that I often have to write something like this:
>
> > if (cond) $(this).show() else $(this).hide();
>
> > Since jQuery is about reducing and chaining, wouldn't it be nice if I
> > could write it like this:
>
> > $(this).toggle(cond); // shows if cond evaluates to true, else hides
>
> > Of course a new function showhide could be made for this, but toggle
> > seems a likely candidate for overloading.
>
> > Not sure if toggle(cond) is the best choice, though. It's already pretty
> > overloaded as it is, and it can already take a string or numeric argument
> > for speed (e.g. 'slow'), which means you'd have to make sure that cond ===
> > true, not just cond == true.
>
> Hmm, I actually checked the docs for that because that would make
> .toggle an unsuitable overloader. The docs said toggle doesn't take
> arguments:
http://docs.jquery.com/Effects> Outdated?
>
> > Maybe a simple plugin would be more appropriate here. Something like this
>
> That would be no problem at all of course.