[Javascript] A better way to do this

Matt Warden mwarden at gmail.com
Tue Apr 29 13:53:23 CDT 2008


On Tue, Apr 29, 2008 at 2:23 PM, tedd <tedd at sperling.com> wrote:
> Hi gang:
>
>  There has to be a better way to do this:
>
>  function checkAll( form )
>         {
>         if(form.checkall.checked)
>                 {
>                 form.a1.checked = true;
>                 form.a2.checked = true;
>                 form.a3.checked = true;
>                 form.a4.checked = true;
>                 form.a5.checked = true;
>                 }
>         else
>                 {
>                 form.a1.checked = false;
>                 form.a2.checked = false;
>                 form.a3.checked = false;
>                 form.a4.checked = false;
>                 form.a5.checked = false;
>                 }
>         }

Making some minor assumptions about your local variable names and the
elements in the form, I might suggest (untested)...

for (var i=0; i<form.elements.length; i++) {
   var e = form.elements[i];
   if (e.id != 'checkall' && e.type == 'checkbox') {
      e.checked = form.checkall.checked;
   }
}

-- 
Matt Warden
Cincinnati, OH, USA
http://mattwarden.com


This email proudly and graciously contributes to entropy.



More information about the Javascript mailing list