PDA

View Full Version : variable inside a variable


loophole
03-25-2003, 06:36 AM
I am trying to put a variable insise form variable to loop through them. I could not get this to work.
Please anyone have any suggestions.


for (var count = 1; count < 6; count++)
{

if ( theForm.n_rms.value > count)
{
var xcnt = count++;
if (("theForm.rm_" + xcnt + "_occu.value" != "CHD") && ("theForm.rm_" + xcnt + "_adt.value" == "0"))
{
alert("Please state the no. of adults for room xcnt\n");

"theForm.rm_" + xcnt + "_adt.focus()";
return (false);
}





}




} // end of loop

I am simply trying to put variable xcnt into the form variable.

Thanks,
Hasan
:confused:

Mhtml
03-25-2003, 06:45 AM
Try it in a function and just pass the form field name that way, no quotes!

pankaj
03-25-2003, 06:51 AM
I have written comments where i fill u might be going wrong

for (var count = 1; count < 6; count++)
{

if ( theForm.n_rms.value > count)
{
var xcnt = count++; // I think ur increamenting the variable here and also in the for loop


// I use eval function whenever I use variables in an elements name so

var obj

obj = eval("theForm.rm_" + xcnt + "_occu")

if ((obj.value != "CHD") && (obj.value == "0"))
{
alert("Please state the no. of adults for room xcnt\n");

"theForm.rm_" + xcnt + "_adt.focus()";
return (false);
}





}

Mhtml
03-25-2003, 06:59 AM
Ahh! Good save pankaj! I Missed that, didn't even see the for loop :eek: ... I think I need some coffee!

loophole
03-25-2003, 07:37 AM
Pankaj......you saved my day ...thanks a lot....
i did not know about eval functions before


thanks so much
Hasan
:thumbsup:

glenngv
03-25-2003, 09:34 AM
as much as possible, i don't use eval()...

obj = eval("theForm.rm_" + xcnt + "_occu");

instead of eval you can also use:

obj = theForm.elements["rm_" + xcnt + "_occu"];

it's more readable, and more appropriate :)