Why is this javascript loop for checking checkbox values not working?

The following form submission function in JavaScript will get the first check box value, but will fail to get others. I have tested this numerous ways, and it seems to always have an issue when getting the ".checked" value for the check box the second time.

Javascript:
function checkForm()
{
  numrows = document.theform.numrows.value;
  if(numrows == -1)
  {
    alert("You do not have any transactions setup yet");
    return false;
  }

  emailcheckcount = 0;
  for(i=0; i<=numrows; i++)
  {
      var recid = document.forms["theform"]["recid"+i].value;
      var checkbox = document.getElementById(recid+"_to"+i);

      if (checkbox.checked)
      {
      emailcheckcount++;

      }
  }

    if(emailcheckcount == 0) {
    alert("Please Select At Least One Email for Alerts");
    return false;
    }

  document.theform.submit();
}

Open in new window


HTML/ASP:

<%
    numrows = -1
    strSQL = "select recid, customer, documenttpid, tolist, cclist, bcclist, receivedvia, addmailout, emailsubject from MailOut where customer like '%"&cname&"%' and (receivedvia = 'email' or addmailout = 'email') and emailsubject <> 'ignore' order by customer, documenttpid;"
    Set rs = objConnection2.Execute(strSQL, ,adCmdText)
    if not (rs.eof and rs.bof) Then

      arr = rs.getrows()
      numrows = ubound(arr,2)
      for i=0 to numrows
        dim tolist
        dim cclist

        recid = arr(0,i)
        customer = arr(1,i)
        documenttpid = arr(2,i)
        if not IsNull(arr(3,i)) Then
          tolist = split(arr(3,i),"|")
        else
          tolist = null
        end if


        if not IsNull(arr(4,i)) and Len(arr(4,i)) > 0 Then
          cclist = split(arr(4,i),"|")
        else
          cclist = null
        end if

        bcclist = arr(5,i)
        receivedvia = arr(6,i)
        addmailout = arr(7,i)
        emailsubject = arr(8,i)


    %>
    <tr>
    <!--
    <td valign='top' style="border-bottom:1px solid black;">&nbsp;<%=customer%></td>
    <td valign='top' style="border-bottom:1px solid black;">&nbsp;<%=receivedvia%></td>
    -->
    <td valign='top' style="border-bottom:1px solid black;">&nbsp;<%=documenttpid%></td>
    <td valign='top' style="border-bottom:1px solid black;">
    <input type="hidden" name="recid<%=i%>" value="<%=recid%>">&nbsp;<%=emailsubject%></td>
    <td valign='top' style="border-bottom:1px solid black;">
    <%
    emailnum = 0
    if not isNULL(tolist) then
        for each email in tolist
          if  not(isNULL(email) or trim(email)="") Then
    %>
      <input type='checkbox' id='<%=recid%>_to<%=emailnum%>' name='<%=recid%>_to<%=emailnum%>'>&nbsp;<%=email%> /<%=recid%>_to<%=emailnum%>
      <input type='hidden' name='<%=recid%>_to<%=emailnum%>email' value='<%=email%>'><BR>
    <%
          end if

          emailnum = emailnum + 1
        next
    end if
    %>
    <input type='hidden' name='numtoemails<%=i%>' value='<%=emailnum%>'>

    <BR>Add Email:<input type='text' name='addtoemail<%=i%>'></td>
    <td valign='top' style="border-bottom:1px solid black;">
    <%
    ccnum = 0
    if(not isNULL(cclist))then
        for each email in cclist
          if trim(email) <> "") = 0 Then
    %>
      <input type='checkbox' name='<%=recid%>_cc<%=ccnum%>'>&nbsp;<%=email%>
      <input type='hidden' name='<%=recid%>_cc<%=ccnum%>email' value='<%=email%>'><BR>
    <%
          end if

          ccnum = ccnum + 1
        next
    end if
    %>
    <input type='hidden' name='numccemails<%=i%>' value='<%=ccnum%>'>

    <BR>Add Email:<input type='text' name='addccemail<%=i%>'></td>
    </tr>
    <%
      next
    end if
    %>

    <input type="hidden" name="numrows" value="<%=numrows%>">

    <tr><td align="Right" colspan="4"><input type="button" value="Submit" style="width:70px;" class="btn-primary" onclick="checkForm();"></td></tr>

Open in new window

Jordan JohnsonAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

Tom BeckCommented:
The javascript you posted has nothing to do with the question from what I can see. In that, you are simply counting the number of checked boxes to see if at least one is checked.

In the HTML/ASP script it appears that you are creating a number of checkboxes.

<input type='checkbox' id='<%=recid%>_to<%=emailnum%>' name='<%=recid%>_to<%=emailnum%>'>&nbsp;<%=email%> /<%=recid%>_to<%=emailnum%>

As long as the checkbox names are unique, the post data should include each checked checkbox.

Have you looked at the source code of the rendered page to see if they are indeed unique?

The alternative, if the names are not unique, is to post an array of checked boxes on submit. Add a "[]" to the end of the name.
Julian HansenCommented:
@Tom
Slight correction
As long as the checkbox names are unique, the post data should include each checked checkbox.
Only the checked items are submitted.

@Jordon, can you show us what the rendered html looks like. I understand what you are trying to do but there are potentially easier ways of doing it for instance with jQuery you can do something like this
$(function() {
  $('form').submit(function() {
    if ($('input:checked').length == 0) {
      alert("Please Select At Least One Email for Alerts");
      return false;
    }
}

Open in new window

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
Tom BeckCommented:
@julian - You quoted me but misread. "Post data will include each checked checkbox", meaning "not all the checkboxes", just the checked ones.
Julian HansenCommented:
@Tommy (blush) apologies - you are 100% correct missed the 'checked'
Julian HansenCommented:
@Jordon - you have given a B grade on this question without supplying a reason for doing so. Please refer to the EE guidelines on grading.
http://support.experts-exchange.com/customer/portal/articles/481419

By default an A grade is given. Refer to the guidelines for more info
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
JavaScript

From novice to tech pro — start learning today.