Hi,

Expanding on DJ's answer (which has a couple of errors, but he was on
his phone!), more generally, if you can get the elements into an
array, you can use Enumerable#invoke[1] to call the same method on
each of the elements.  So for instance, if you used DJ's suggestion
and gave them all the same class, it becomes:

$$('.theclass').invoke('hide');

Tidy, eh?  Now, if you need to do it with a disparate set of elements,
you can always use an array literal:

[ 'ErrorConsumerEmail0address',
  'ErrorConsumerfirstname',
  'ErrorConsumerlastname',
  'ErrorConsumerPhone0Number'
].invoke('hide');

If you *really* want to squeeze down the character count, you can use
$w[2] to create the array:

$w('ErrorConsumerEmail0address ErrorConsumerfirstname
ErrorConsumerlastname ErrorConsumerPhone0Number').invoke('hide');

$w (note that it's lower case; it's the only lower case one of the $
functions) splits a string into an array on whitespace.  Adds a tiny
bit of overhead and saves a few keystrokes.

There's more about looping arrays in the unofficial wiki[3].  It is
also well worth your time to take an hour and read through the
Prototype API[4] front to back.  It literally takes an hour or so, and
it pays you back very very quickly.

[1] http://prototypejs.org/api/enumerable/invoke
[2] http://prototypejs.org/api/utility/dollar-w
[3] http://proto-scripty.wikidot.com/prototype:tip-looping-through-arrays
[4] http://prototypejs.org/api

HTH,
--
T.J. Crowder
tj / crowder software / com
Independent Software Engineer, consulting services available

On Aug 7, 6:59 pm, trope <jtrope...@gmail.com> wrote:
> I have some code that appears to be a prime candidate for a loop.
>
> How could I wrap this up in one neat little function???
>
> /* Hide previous errors */
>         $('ErrorConsumerEmail0address').hide();
>         $('ErrorConsumerfirstname').hide();
>         $('ErrorConsumerlastname').hide();
>         $('ErrorConsumerPhone0Number').hide();
>
> Thank you.
>
> Trope!
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Prototype & script.aculo.us" group.
To post to this group, send email to prototype-scriptaculous@googlegroups.com
To unsubscribe from this group, send email to 
prototype-scriptaculous+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to