How can I retrieve the value of this checkbox in Javascript?

I am trying to retrieve the value, of whether the checkbox is checked or not, but it is not working. When I retrieved the value it just says "on" whether or not it is checked. Can someone help point me in the right direction? Thanks!

<script language="javascript">
function checkForm()
{
  
  emailcheckcount = 0;
  for(i=0; i<=numrows; i++)
  {
	  var recid = document.forms["theform"]["recid"+i].value;
	  
	  
    if(document.theform.recid+"_to"+i.checked) {
        emailcheckcount++;
    }
  
    if(emailcheckcount == 0) {
    alert("Please Select At Least One Email for Alerts");
	return false;
    }
  
  document.theform.submit();
}
</script>

<input type='checkbox' name='<%=recid%>_to<%=emailnum%>'>

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.

Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
>><input type='checkbox' name='<%=recid%>_to<%=emailnum%>'>
may we know what's the output generated from this?

>>for(i=0; i<=numrows; i++)
did you save the value of numrows in your Javascript as well?

I believe this is not the full scripts to generate the output page?
Jordan JohnsonAuthor Commented:
I took out some of the code, sorry. It does get the numrows

numrows = document.theform.numrows.value;

Open in new window


The output looks like this for the checkbox name ie - "19181_to0"
Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
assuming below is the output that dynamically generated, with the modified javascript validation:

<script language="javascript">

function checkForm(frm)
{
  var emailcheckcount = 0;
  var numrows = frm.numrows.value;
  
  for(i=0; i<numrows; i++)
  {
    var ctrl = document.getElementById("19181_to"+i);
	  
    if(ctrl.checked) {
        emailcheckcount++;
    }    
  }
  if(emailcheckcount == 0) {
      alert("Please Select At Least One Email for Alerts");
      return false;
  }
  return true;
}

</script>


<form name="theform" action="process" onsubmit="return checkForm(this)">

<input type='checkbox' name='19181_to0' id='19181_to0'> Test 0<br>
<input type='checkbox' name='19181_to1' id='19181_to1'> Test 1<br>
<input type='checkbox' name='19181_to2' id='19181_to2'> Test 2<br>
<input type='checkbox' name='19181_to3' id='19181_to3'> Test 3<br>

<input type="hidden" name="numrows" value="4">
<input type="submit" value="Submit">

</form>

Open in new window

Pls customize accordingly
zzynxSr. Software engineerCommented:
Isn't this a very string thing to write?:
document.theform.recid+"_to"+i.checked

Open in new window

In analogy with the line above it, shouldn't that be:
document.theform.[recid+"_to"+i].checked

Open in new window

or
document.forms["theform"]["recid_to"+i].checked

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