|
|
|||||||||
|
|||||||||
|
|||||||||
| |
||
| ||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
||||
|
||||
|
I'm using a for loop in Javascript and I'm trying to loop through a set of checkbox objects. Does anyone know what syntax I should be using please? The objects are labelled as, chkGKPlayer1, chkGKPlayer2, chkGKPlayer3, etc... and appear in the form frmUpdate.
My loop looks like this: (???'s indicate the bit I'm unsure of) // var count For (count=1 ; count<=limit ; count++) { If (document.frmUpdate.?????.checked==true) {alert("checked");}} // I was thinking that the question marks should be filled in by something like this... ["chkGKPlayer" + count] ...but it doesn't work stating "expected identifier" Can anyone help? (I'm basically trying to test if each of the checkboxes chkGKPlayer1, chkGKPlayer2, ..., chkGKPlayer(Count!!!!!) are checked) Pleeeasse help. Thank yooooo |
|
#2
|
||||
|
||||
|
checkboxes of the same group should have the same name, with different value. that way getting the list of selected values is much more simple. in such case, have this code:
Code:
var count=0;
var chkGroup=document.frmUpdate.chkGKPlayer;
for (var i=0 ; i<chkGroup.length; i++)
{
if (chkGroup[i].checked)
{
alert("checked: "+chkGroup[i].value);
}
count++;
}
if you insist on having different name for each checkbox, use such code: (limit must be defined and accurate!) Code:
var count=0;
var chkName="chkGKPlayer";
for (var i=1 ; i<=limit; i++)
{
var curBox = document.frmUpdate.elements[chkName+i];
if (curBox.checked)
{
alert("checked: "+curBox.value);
}
count++;
}
note, you must not assign value to variable via loop. variable used as loop iterator is not stable and might lose its value once the loop is over. |
|
#3
|
||||
|
||||
|
Thanks for this. I'll give it a go later. (You have taught me some new notation and how to cut down the text. Thanks.)
For Info Purposes: To assign the value of the variable "limit" in my loop I will use: <%Response.Write i%> Where i is the value of a counter that has been accumlated as I write each of the check boxes from the data in my recordset. |
|
#4
|
||||
|
||||
|
ok, fair enough:
for (var i=1; i<=<%=i%>; i++) glad I could help! ![]() |
![]() |
| Viewing: ASP Free Forums > Programming > HTML, JavaScript And CSS Help > Syntax - Java Loop? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|