Forum Moderators: open
The output I get:
Input 0 : One;
Input 1 : One;
Input 2 : One;
Input 3 : One;
.
.
.
function createForm(number) {
data = "";
var val = new Array (number);
for(k=0;k<number;k++){
val[k] = "One Two Three";
}
if (number > -1) {
for (i=0; i < number; i++) {
data = data + "Input " + i + " :" + "<input type='text' size=20 name=" + "txt" + i + "value=" + val[i] + "'><br><br><br>";
}
if (document.all) {
cust.innerHTML = data;
}
}
}
Assuming i is 1, and val is "Foo Bar" for this example.
This code:
data = data + "Input " + i + " :" + "<input type='text' size=20 name=" + "txt" + i + "value=" + val + "'><br><br><br>";
Can firstly be shortened to:
data += "Input "+ i +" :<input type='text' size=20 name=txt"+ i +"value="+ val +"'><br><br><br>";
Where you have "some"+" text" is a bit pointless as this is just the same as "some text", so I've taken those bits out, and at the beginning used "data +=" instead.
ok, now take the values of i and val and it works out to:
data += "Input 1 :<input type='text' size=20 name=txt1value=Foo Bar'><br><br><br>";
You can see where the problem is now, it's basically right, you're just missing a space before "value", and the single quote after "value=". Also it's a good idea to add single quotes around name too.
The code should read:
data += "Input "+ i +" :<input type='text' size=20 name='txt"+ i +"' value='"+ val[i] +"'><br><br><br>";
Try that.