Click to See Complete Forum and Search --> : A few questions...


AdamBrill
12-08-2002, 10:00 PM
I have a question for you guys... Here is some code:

<html>
<script language=javascript>
document.write("<Form>");
for(x=0;x<3;x++)
{
document.write("<input type='button' Name='test'+x onclick='OpenLink(x)' value='value'>\n");
}
document.write("</Form>");
function OpenLink(x)
{
alert(x)
}
</script>
</html>

It generates three buttons and runs the OpenLink function when you click on one. The problem is that all of the buttons return what x is currently at (3) when the button is clicked. I wan't them to return the number of the button that was clicked. I hope that wasn't to confusing... :) Let me know if you don't know what I mean... Thanks, Adam

Beach Bum
12-08-2002, 10:29 PM
it is obvious why it is doing what you say.

you are counting to three while generating three buttons.

then after the count is three you are using that same variable for a result. of course it will always be three.

now for as to what you want . . . you need to use the value of x rather than the variable x where you have openLink(x) in your document.write.

jeffmott
12-08-2002, 11:36 PM
document.write("<input type='button' Name='test'+x onclick='OpenLink(x)' value='value'>\n");

This line needs to be changed to

document.write("<input type='button' name='test" + x + "' onclick='OpenLink(" + x + ")' value='value'>\n");

AdamBrill
12-09-2002, 07:26 AM
That worked, thanks a lot. It saved me a lot of time... :) Thanks again, Adam