[jQuery] Re: show/hide/toggle: suggestion to reduce show/hide code

2008-11-19 Thread Hector Virgen
I ran into this same problem today and wrote up this quick plugin that accepts a boolean or function: $.fn.extend({ showIf: function(fn) { var result; switch (typeof fn) { case 'function': result = fn.call(this); break; default: result = fn; } if (result) { $(this.show()); } else {

[jQuery] Re: show/hide/toggle: suggestion to reduce show/hide code

2008-11-19 Thread Hector Virgen
Oops, fixed a typo: $.fn.extend({ showIf: function(fn) { var result; switch (typeof fn) { case 'function': result = fn.call(this); break; default: result = fn; } if (result) { $(this).show(); } else { $(this).hide(); } return $(this); } }); -Hector On Wed, Nov 19, 2008 at 9:29 AM, Hector

[jQuery] Re: show/hide/toggle: suggestion to reduce show/hide code

2008-11-18 Thread Karl Swedberg
On Nov 18, 2008, at 1:10 AM, Dylan Verheul wrote: I'm building a new version of an often used data entry form on our site Waarneming.nl (int'l version Observado.org). It struck me that I often have to write something like this: if (cond) $(this).show() else $(this).hide(); Since jQuery is

[jQuery] Re: show/hide/toggle: suggestion to reduce show/hide code

2008-11-18 Thread Dylan Verheul
On Tue, Nov 18, 2008 at 14:56, Karl Swedberg [EMAIL PROTECTED] 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:

[jQuery] Re: show/hide/toggle: suggestion to reduce show/hide code

2008-11-18 Thread MorningZ
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

[jQuery] Re: show/hide/toggle: suggestion to reduce show/hide code

2008-11-18 Thread Karl Swedberg
On Nov 18, 2008, at 9:02 AM, Dylan Verheul wrote: On Tue, Nov 18, 2008 at 14:56, Karl Swedberg [EMAIL PROTECTED] wrote: 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.

[jQuery] Re: show/hide/toggle: suggestion to reduce show/hide code

2008-11-18 Thread Dylan Verheul
On Tue, Nov 18, 2008 at 19:58, Karl Swedberg [EMAIL PROTECTED] wrote: On Nov 18, 2008, at 9:02 AM, Dylan Verheul wrote: Hmm, I actually checked the docs for that because that would make .toggle an unsuitable overloader. The docs said toggle doesn't take arguments: Hmmm, indeed. :) Not so