I want to use counter variable in IF condition..I tried but its not working.Please help me with correct code..
code is:
var ChForAtn=0;
if(arrayAtnd[ChForAtn]=="Name")
{
alert("Name");
if(document.forms[7].ckbx'+ChForAtn+'.checked==true)
{
alert('Name added and checked also');
}
ChForAtn++;
}
First off, you should post your loop. The code snippet you posted doesn’t contain a loop.
The only incremented variable you have in this code is reset at the top of the code, which doesn’t make much sense for this fragment.
EDIT
The following won’t work because you have your concatenation mixed up
I’m not any too good with JS, but if you’re trying to make a variable variable like in PHP, you’d have to use something like
eval(). I know you can access it viawindow[]but this might not be possible in this case.A solution with
eval()NOTE There are good reasons why you shouldn’t use
eval(). Have a look here for more information : https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/eval#Don.27t_use_eval.21However, given your use case I don’t really see a way around it. I would welcome alternative suggestions from other posters.
EDIT2
As @nickf points out in the comments, you can do the same thing by calling
A cautionary tale for others, don’t use eval() where avoidable 🙂