Forum Moderators: open

Message Too Old, No Replies

For loop with innerHTML

Tough Problem

         

treeleaf20

7:13 pm on Jul 28, 2004 (gmt 0)

10+ Year Member



Hey all,
I have this problem. I have a form and based on a number a user puts in there I want to print out innerHTML the number of rows that the user selects. Here is the code:
function gettextfieldnumber(){
if (formTool.textfieldbox.checked){
document.getElementById("textfieldnumber").innerHTML = "<input name='textfields' type='text' size='20'><input type='button' name='build' value='Submit' onClick='javascript:showtextfield(formTool.textfields.value);'>";
}
if (!(formTool.textfieldbox.checked)){
document.getElementById("textfieldnumber").innerHTML = "";
document.getElementById("textfield").innerHTML = "";
}
}
function showtextfield(size){
if (size > 0)
{
var i;
for (i=0; i<size; i++)
{
//alert ('it works')
document.getElementById("textfield").innerHTML = "<table><tr> <td width='120'><b>Field Name</b></td><td width='10'>&nbsp;</td><td width='70'><b>Required?</b></td><td width='10'>&nbsp;</td><td width='120'><b>Value</b></td><td width='10'>&nbsp;</td><td width='134'><b>Length</b></td><td width='10'>&nbsp;</td><td width='50'><div id='howlong1'></div></td></tr><tr> <td><input type='text' name='fieldtextname1' value='' size='20'></td><td width='10'>&nbsp;</td><td width='70'><select name='requiredtext1'><option selected>Select</option><option value='Yes'>Yes</option><option value='No'>No</option></select></td><td width='10'>&nbsp;</td><td><input type='text' name='valuetext1' value='' size='20'></td><td width='10'>&nbsp;</td><td width='134'> <label> Empty <input type='radio' name='RadiotextGroup1' value='empty' onClick='hidelength(1)'>Length</label> <label><input type='radio' name='RadiotextGroup1' value='length' onClick='showlength(1)'></label></td><td width='10'>&nbsp;</td><td width='19'><div id ='lengthbox1'></div></td></tr></table>";
}
}

As it is now it just overwrites it everytime...instead I would like it to print out rows below that for the amount of times the user selected. So for example if a user selects 10 it should print out that 10 times, now it just over writes it 10 times and prints out once. Anyone have any idea how I can do this. Thanks in advance!

treeleaf20

7:17 pm on Jul 28, 2004 (gmt 0)

10+ Year Member



Also somehow if someone could show how to increment the field names by 1 which the code will display that would be great as well....for example for the first loop through call it textfieldname1 and for the second iteration call it textfieldname2 and so on...

Rambo Tribble

10:34 pm on Jul 28, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Instead of making your innerHTML value = to the content, use += to concatenate it.

For incrementing your textfield name just set a global variable and increment it with each iteration, and use the string concatenation + to add the global's value to the base string (JS will automatically convert the numeric value of the variable to a string).