Cycle through checkboxes on a form and determine if at least one has been checked

I have a form with 3 checkboxes.  I need the button to be disabled until the user clicks one of the boxes, at which point it will become enabled.  If the user clicks a checkbox, and then clicks again (un-checking it), i need to cycle through all the checkbxes to see whether or not one is still checked in order to set the correct state of the buttons.  What I cant figure out is the javacript syntax to check the value of all the checkboxes in a form after an onClick event, or something similar?

<form name="form1">

<input type="checkbox" value="1">1
<input type="checkbox" value="2">2
<input type="checkbox" value="3">3

<input type="button" disabled value="Go">
</form>
soBCAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

minichickenCommented:
<script language = "javascript">
function chkbutton()
{
      if (form1.chk1.checked == true || form1.chk2.checked == true || form1.chk3.checked == true)
      {
            form1.thebutton.disabled = false;            
      }
      else
      {
            form1.thebutton.disabled = true;
      }
}
</script>
<form name="form1">

<input name ="chk1" type="checkbox" value="1" onclick ="chkbutton();">1
<input name ="chk2" type="checkbox" value="2" onclick ="chkbutton();">2
<input name ="chk3" type="checkbox" value="3" onclick ="chkbutton();">3

<input name ="thebutton" type="button" disabled value="Go">
</form>
0
soBCAuthor Commented:
Ah...i have one more condition actually.  The number of checkboxes may not always be 3.  I need to account for that variation.  Thanks for the sample though.  It'll work fine statically, but Im not sure how I'd modify it to handle dynamically generated lists of checkboxes...
0
nan1217Commented:
Loop through all form elements but add this

if (form1[i].type == "checkbox")

and then only work with those that evaluate as true.  So it would look something like this (code is untested):

<script language="JavaScript" type="text/javascript">
function uncheck(form) {
var i;
var formElements = form.elements;

for (i=0; i<formElements.length; i++) {  //loop through form elements
     if (formElements[i].type == "checkbox") {  //test for only checkbox items
          if (formElements[i].checked) {  //test if element is checked
               form1.thebutton.disabled = true;  //uncheck
          }
     }
}
}
</script>
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
minichickenCommented:
<script language = "javascript">
function chkbutton()
{
      document.form1.thebutton.disabled = true;
      
      for(var i=0; i< document.form1.elements['chk[]'].length;i++)
      {
            if(document.form1.elements['chk[]'][i].checked)
            {
                  document.form1.thebutton.disabled = false;
            }
      }
}



</script>
<form name="form1">

<input name ="chk[]" type="checkbox" value="1" onclick ="chkbutton();">1
<input name ="chk[]" type="checkbox" value="2" onclick ="chkbutton();">2
<input name ="chk[]" type="checkbox" value="3" onclick ="chkbutton();">3

<input name ="thebutton" type="button" disabled value="Go">
</form>
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Development

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.