Hi,
I'm writing a script and am having trouble referencing text boxes using getElementById. If I pass the id as a variable it fails.
So...
document.getElementById("ABC").value; works but
var useMe = "ABC";
document.getElementById(useMe).value; does not work
I've included the important parts of what I've coded so far. Its slightly complicated but here goes.
The number of text boxes being created depends on two variables. I have used PHP to create nested for loops with an echo command to output the boxes...
$var1; // Is an array, holding various strings
$numVar1 = count($var1);
$var2; // Also an array, holding various ints number of elements always equal to $numVar1
for ($i = 0; $i < $numVar1; $i++)
{
for ($j = 0; $j < $var2[$i]; $j++)
{
echo "<input type=\"text\", name=\"$var1[$i]$j\" id=\"$var1[$i]$j\">";
}
}
I have tested the loop out and the values entered for name and id are as I want them to be. e.g.
If $var1={A,B,C} $var2={2,3,1}
I get text boxes with name and id... A0,A1,B0,B1,B2,C0
Later I try to reference the values in the text boxes. This is done using JavaScript. The variables are equivalent to the PHP variables used above.
for(var i = 0; i < numVar1; i++)
{
for(var j=0; j < var2; j++)
{
var getID = var1 + j;
var getVal = document.getElementById(getID).value;
}
}
The var getVal line is where my code fails. The value for getID is always as it should be. i.e. A0, B3, C1 etc...
I've tried assigning getID as an id for a text box that wasnt in the PHP loop and that worked as expected. I also tried hard coding in an id for a box that was in the PHP loop, and that worked as expected. To me it seems as if the variables I pass for the id are wrong, but as I stated before, I checked them and they aren't.
I hope I've explained what I'm trying to do clearly.
Any ideas?
Thanks,
James
I'm writing a script and am having trouble referencing text boxes using getElementById. If I pass the id as a variable it fails.
So...
document.getElementById("ABC").value; works but
var useMe = "ABC";
document.getElementById(useMe).value; does not work
I've included the important parts of what I've coded so far. Its slightly complicated but here goes.
The number of text boxes being created depends on two variables. I have used PHP to create nested for loops with an echo command to output the boxes...
$var1; // Is an array, holding various strings
$numVar1 = count($var1);
$var2; // Also an array, holding various ints number of elements always equal to $numVar1
for ($i = 0; $i < $numVar1; $i++)
{
for ($j = 0; $j < $var2[$i]; $j++)
{
echo "<input type=\"text\", name=\"$var1[$i]$j\" id=\"$var1[$i]$j\">";
}
}
I have tested the loop out and the values entered for name and id are as I want them to be. e.g.
If $var1={A,B,C} $var2={2,3,1}
I get text boxes with name and id... A0,A1,B0,B1,B2,C0
Later I try to reference the values in the text boxes. This is done using JavaScript. The variables are equivalent to the PHP variables used above.
for(var i = 0; i < numVar1; i++)
{
for(var j=0; j < var2; j++)
{
var getID = var1 + j;
var getVal = document.getElementById(getID).value;
}
}
The var getVal line is where my code fails. The value for getID is always as it should be. i.e. A0, B3, C1 etc...
I've tried assigning getID as an id for a text box that wasnt in the PHP loop and that worked as expected. I also tried hard coding in an id for a box that was in the PHP loop, and that worked as expected. To me it seems as if the variables I pass for the id are wrong, but as I stated before, I checked them and they aren't.
I hope I've explained what I'm trying to do clearly.
Any ideas?
Thanks,
James