[thelist] JavaScript: check all boxes within a table

Joshua Olson joshua at waetech.com
Thu Feb 13 08:51:01 CST 2003


----- Original Message -----
From: "Bill Haenel" <mail at webmarketingworx.com>
Sent: Wednesday, February 12, 2003 10:55 PM


> > it'd be a trivial task if the names of the checkboxes were
> > unique to each table.
>
> I'm actually trying to avoid this, because it makes coding on the
> receiving end of the form process more complicated. i.e., instead of
> running through an array and handling each key/value, I'd have to run
> through an array for each individual table. That would work nicely,
> though, for the 'checkall' part, wouldn't it?

How about something like this:

// Find all checkboxes within the table and store them into
// an array.

table = document.getElementById('mytablesid');
inputs = table.getElementsByTagName('input');
var checkboxes = new Array();
for (i = 0; i < inputs.length; i++)
{
  if (!inputs[i].length)
  {
    if (inputs[i].type == 'checkbox')
      checkboxes[checkboxes.length] = inputs[i];
  } else
  {
    for(k = 0; k < inputs[i].length; k++)
    {
      if (inputs[i][k].type == 'checkbox')
        checkboxes[checkboxes.length] = inputs[i];
    }
  }
}

// checkboxes now is an array of all checkboxes
// in the table with id "mytablesid".  Now perform some
// action on the checkboxes.  In this case, check
// them all.

for (i = 0; i < checkboxes.length; i++)
  checkboxes[i].checked = true;

HTH,
-joshua




More information about the thelist mailing list