Forum Moderators: open

Message Too Old, No Replies

check multiple checkbox groups in one form

         

louisk

10:22 am on Apr 25, 2006 (gmt 0)



Hi All, I am searching this forum but cant find what I'm looking for, so here goes.

What I want to do is have one checkbox select more checkboxes. But that’s simple, I did find that on this forum :)
What I have is a page with 3 groups of 5 checkboxes. Each group has to have a checkbox that checks all the checkboxes in only its own group. But they have to be in one and the same form...

What I am actually looking for is a (java?) script to select multiple checkboxes independent of the form it is in. For example one checkbox checks all checkboxes with id=1..... another checkbox checks all checkboxes with id=2... despite of them being in the same or in different forms... so I don’t want the form name to play a part in the script.

Can anyone help me?

BlobFisk

11:52 am on Apr 25, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That's quite straight forward. Since you are grouping your checkboxes you put them in a container (fieldset, div, table whatever) and use that to get all of the checkboxes.

Build an array of all the inputs in the container:

inputArray = document.getElementById('container').getElementsByTagName('INPUT');

If you have other inputs then you can use a class on the checkboxes and use that to select the checkboxes:


for(i=0;i<inputArray.length;i++) {
if(inputArray[i].className=='checkboxClassName') {
inputArray[i].checked = 'checked';
}
}

That is the principle at it's most basic. You can make the scipt more intellegent by seeing if they are checked and if so unchecking them (and vice versa). You can do the same on the control checkbox, if it is checked then uncheck everything and again vice versa.

HTH