Forum Moderators: open

Message Too Old, No Replies

Dynamic Input - Text Field Value Problem

         

Mekazama

3:44 am on Apr 24, 2007 (gmt 0)

10+ Year Member



The function works perfectly to display the text field on the I.E but the problem is the value displayed for each text field is incorrect. It will ignore the string once it found space. I'm really out of ideas.Any taught..anyone?.

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;
}
}
}

Dabrowski

5:13 pm on Apr 24, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's because you have no quotes around your value.

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.

Mekazama

12:11 am on Apr 25, 2007 (gmt 0)

10+ Year Member



Thanks Dabrowski, I just added the single quote and it works perfectly. Thanks once again for helping me out. :)

Dabrowski

12:31 am on Apr 25, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



A pleasure.