Which Checkbox checked?

Results 1 to 4 of 4

Thread: Which Checkbox checked?

  1. #1
    Jperry Guest

    Default Which Checkbox checked?

    Is there a way to get the index value or name of which checkbox was checked out of several on the form?<BR><BR>Say I have checkbox_1, checkbox_2, ....... checkbox_20 and checkbox_8 is checked I just want to be able to capture the index number or the checkbox&#039;s name.<BR><BR>Thanks for your help.

  2. #2
    emets Guest

    Default RE: Which Checkbox checked?

    Checkboxes expose an updateable .checked property. If your checkboxes have completely unique names, you can access their .checked properties directly:<BR><BR>if (form1.ckeckbox1.checked) ... ;<BR><BR>Same-name groups, as in you example are treated as arrays, like this:<BR><BR>if (form1.checkgroup[0].checked) ... ;<BR><BR>So you could probably write a function that goes round the checkboxes and returns their index if checked.<BR>

  3. #3
    Jperry Guest

    Default RE: Which Checkbox checked?

    Ok. One thing I forgot to mention is that I will not know ahead of time how many checkboxes there will be. So in javascript how would I loop through them and get the indexnumber or name of the first checkbox that is checked?

  4. #4
    rhop Guest

    Default RE: Which Checkbox checked?

    If you don&#039;t know how many there will be, you will may not know the names either, so you will need to cycle through all of the existing checkboxes on the page.<BR><BR>In JavaScript, the "form" object contains the "elements" object array which is an array of all of the elements in that particular form. You can then loop through that array to check each element. The only problem? The elements array includes all elements, not just checkboxes.<BR><BR>However, the "elements" object has a "type" property that specifies what type of element it is ... text, checkbox, etc.<BR><BR>So what you want to do is loop through the array of elements. In the loop, do a test to see if :<BR><BR>----&#062; form.elements[i].type=="checkbox" &#060;----<BR><BR>If it does, then do a second test using the "checked" property to determine if that particular checkbox is checked. If it&#039;s not a checkbox, then you won&#039;t bother with it; if it is a checkbox, you can determine if it is checked.

Posting Permissions

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