View Full Version : testing checkboxes with variable names

11-21-2010, 06:56 PM
I have a form with several types of checkboxes, and I'm trying to check all but one of them to determine if any of those have been checked. I do not want to check 'all', but I want to see if any of the 'check_' or 'check2_' checkboxes have been checked. There are a variable number of checkboxes, so this is just an example with two of each. Set 1 and Set 2 will always have the same number of checkboxes as each other, though.

I'm having trouble getting the function to do this - so any guidance would be appreciated.

<script language="JavaScript">
function checkForNone() {
for (i=0,n=selectForm2.elements.length;i<n;i++) {
if (document.selectForm2.getElementById("check_"+String(i)).checked || document.selectForm2.getElementById("check2_"+String(i)).checked ) {
if (checked==true) {
return true;
else {
alert("Please select at least one checkbox.");
return false;

echo "
<form name='selectForm2' id='selectForm2'>

<input type='checkbox' name='all' value='' class='resultsAll' id='all'>

Set 1:
<input type='checkbox' name='check_1' value='01' class='results1' id='check_1'><br>
<input type='checkbox' name='check_2' value='02' class='results1' id='check_2'><br>

Set 2:
<input type='checkbox' name='check2_1' value='01' class='results2' id='check2_1' disabled='true'><br>
<input type='checkbox' name='check2_2' value='02' class='results2' id='check2_2'>
Check for none selected <input type='button' onclick=\"checkForNone();\" />


Philip M
11-21-2010, 07:35 PM
This should point you the right way:-

<script type = "text/javascript">
function Check(chk) {
var totChecked = 0;
for (i = 0; i < chk.length; i++) {
if (chk[i].checked == true) {
totChecked ++
alert ("Total checked in this set = " + totChecked)

<form name="myform" >

<input type="checkbox" name="check_list1" value="1">RED<br>
<input type="checkbox" name="check_list1" value="2">BLUE<br>
<input type="checkbox" name="check_list1" value="3">GREEN<br>
<input type="checkbox" name="check_list1" value="4">YELLOW<br>

<input type="button" value="Count" onClick="Check(document.myform.elements['check_list1'])">
<input type="checkbox" name="check_list2" value="1">MEAT<br>
<input type="checkbox" name="check_list2" value="2">FISH<br>
<input type="checkbox" name="check_list2" value="3">VEGETABLES<br>
<input type="checkbox" name="check_list2" value="4">FRUIT<br>

<input type="button" value="Count" onClick="Check(document.myform.elements['check_list2'])">

Lottery: A tax on people who are bad at math.

11-21-2010, 07:54 PM
Thanks. I can see how your logic works if all the checkboxes in a set have the same name (like name="check_list1"), but I think part of my problem has to do with the fact that the name and ID on each row are different from the next row in the set (check_1, check_2, check_3), and I don't have the construct right for selecting these elements in order to evaluate each one as it goes through the loop a variable number of times.

Philip M
11-21-2010, 08:03 PM
There is nothing to stop you using a different id for each checkbox (and indeed each id must be unique). But all the checkboxes in a group or set should have the same name. That is required because you want to loop through an array a variable number of times.

11-21-2010, 08:22 PM
Thanks for your help, Philip.

11-21-2010, 10:48 PM
I spoke too soon.

The function does work inJavaScript, but when I went back and changed the names of checkboxes to one name in my PHP app, all sorts of things blew up, mainly because I depend on the POSTed names of these variables in PHP.

I can't go back and rework the PHP just to make this JavaScript work, so I'll need to find a way in JavaScript to work off of the checkboxes with incremental variables names (check_1, check_2, etc.). I'm pretty sure there are ways to do this. I've just been having trouble making it work.

Thanks to anyone who may know how to do this...

Old Pedant
11-21-2010, 11:15 PM
For PHP to handle groups of same-named fields, you just have to append [] to the name of the fields.


<input type="checkbox" name="check_list1[]" value="1">RED<br>
<input type="checkbox" name="check_list1[]" value="2">BLUE<br>
<input type="checkbox" name="check_list1[]" value="3">GREEN<br>
<input type="checkbox" name="check_list1[]" value="4">YELLOW<br>
<input type="button" value="Count" onClick="Check(this.form.elements['check_list1[]'])">

Then you can easily alter the PHP code to use

in place of

If that's really too difficult, then we *can* hack a fix in JS:

<input type="checkbox" name="check_list1_1" value="1">RED<br>
<input type="checkbox" name="check_list1_2" value="2">BLUE<br>
<input type="checkbox" name="check_list1_3" value="3">GREEN<br>
<input type="checkbox" name="check_list1_4" value="4">YELLOW<br>

<input type="button" value="Count" onClick="Check(this.form,'check_list1');">

function Check( form, basename )
var count = 0;
for ( var i = 1; i < 9999; ++i )
var fld = form.elements[basename + "_" + i];
if ( fld == null ) break; // quit when no more of that basename
if ( fld.checked ) ++count;
alert( basename + " has " + count + " checked";
return count; // if you want to use it elsewhere

Philip M
11-22-2010, 11:57 AM
alert( basename + " has " + count + " checked" );

11-22-2010, 04:28 PM
This works. Thanks to both Old Pedant and Philip M for your help.