CheckBox Problem

1. Member
Join Date
Dec 1969
Posts
55

## CheckBox Problem

I&#039;ve got a form with a a number of checkboxes (depending on a queries results), I want to restrict the user to clicking a maximum of 3 boxes. I&#039;ve written the function below, but I keep getting an error, can some one point me in the right direction ? <BR><BR>Many Thanks.<BR><BR>FUNCTION<BR>function CheckValues( CheckBoxName )<BR> var i<BR> i=0<BR>{ <BR> for ( var i = 0; i &#060; CheckBoxName.length; ++i ) <BR>{ <BR> if ( CheckBoxName [i].checked ) i=i+1 <BR>} <BR> return i;} <BR><BR>CALL<BR>if ( CheckValues( document.Volunteer.Project ) &#062;3 )<BR>{<BR>alert("More than 3 sessions have been Selected.");<BR> return false;<BR>}

2. Senior Member
Join Date
Dec 1969
Posts
96,118

## You need *TWO* variables

Consider what your code is doing.<BR><BR>Let&#039;s say you had 5 checkboxes and the user checked the first 3.<BR><BR>So 0, 1, and 2 are checked.<BR><BR>And your code does this:<BR> for i = 0 // starting out<BR> if box[0] is checked i=i+1 // so now i is 1<BR> ++i // from the for loop! now i is 2<BR> if box[2] is checked i=i+1 // so now i is 3<BR> ++i // now i is 4<BR> if box[4] is checked // it&#039;s not<BR> ++i // now i is 5...loop is done<BR><BR>So you only actually tested 2 out of the 3 checked boxes and, no matter what, you will return from the function with i=5, so you will *ALWAYS* return 5!<BR><BR>Now try this:<BR><BR>function CheckValues( CheckBoxName )<BR>{ <BR> var chkd = 0;<BR> for ( var i = 0; i &#060; CheckBoxName.length; ++i ) <BR> { <BR> if ( CheckBoxName[i].checked ) ++chkd;<BR> } <BR> return chkd;<BR>} <BR><BR>CALL<BR>if ( CheckValues( document.Volunteer.Project ) &#062;3 )<BR>{<BR> alert("More than 3 sessions have been Selected.");<BR> return false;<BR>}<BR>return true; // don&#039;t omit this!<BR><BR> <BR>

3. Senior Member
Join Date
Dec 1969
Posts
185

## RE: CheckBox Problem

problem 1. you have all many syntax errors like ++i.<BR>problem 2. i&#039;m not sure .checked will work when all the checkboxes have the same name.<BR><BR>try this instead of CheckValues(). it may not work because it counts ALL the checks on the page.<BR><BR><BR>function countChecks(){<BR><BR> var iCount = 0;<BR><BR> for(var i=0; i &#060; document.Volunteer.elements.length; i++) <BR> if(document.Volunteer.elements[i].type == "checkbox")<BR> if(document.Volunteer.elements[i].checked)<BR> iCount++;<BR><BR> return iCount;<BR>} <BR><BR>

4. Senior Member
Join Date
Dec 1969
Posts
96,118

## Well, that's 100% wrong

&#062; problem 1. you have all many syntax errors like ++i.<BR><BR>WHAT are you talking about??? What&#039;s wrong with ++i???<BR><BR>It&#039;s actually a tiny bit more efficient than i++, in fact.<BR><BR>***********<BR><BR>&#062; problem 2. i&#039;m not sure .checked will work when all the checkboxes have the same name.<BR><BR>*READ* his code! He is passing in a reference to the ENTIRE GROUP of checkboxes. Which is automatically converted to an *array* by the DOM.<BR><BR>He was doing <BR> checkboxGroupName[ whichCheckboxInTheGroup ].checked<BR>and that is *absolutely* the correct thing to do.<BR><BR>*********************<BR><BR>Your code doesn&#039;t work if the user has any checkboxes that are *NOT* part of this group on his/her form!<BR><BR>Suppose he has:<BR><BR>&#060;FORM...&#062;<BR>&#060;INPUT Type=Checkbox Name="FOOD" ...&#062;<BR>&#060;INPUT Type=Checkbox Name="FOOD" ...&#062;<BR>&#060;INPUT Type=Checkbox Name="FOOD" ...&#062;<BR>&#060;INPUT Type=Checkbox Name="FOOD" ...&#062;<BR><BR>&#060;INPUT Type=Checkbox Name="PETS" ...&#062;<BR>&#060;INPUT Type=Checkbox Name="PETS" ...&#062;<BR>&#060;INPUT Type=Checkbox Name="PETS" ...&#062;<BR>...<BR>&#060;/FORM&#062;<BR><BR>Your code would count the FOOD and the PETS together.<BR><BR><BR><BR>

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•