Forum Moderators: open

Message Too Old, No Replies

For each Loop

Can JS do this?

         

treeleaf20

2:17 pm on Jul 29, 2004 (gmt 0)

10+ Year Member



Ok I have this code:
function showtextfield(value1)
{
if (value1 > 10){
alert ('Please enter a value that is equal to or less than 10')
}else{
var showQuantity = new Array();
if (value1 > 0)
{
var i;
for (i=0; i<value1; i++)
{
var j = i+1;
showQuantity = "showQuantity"+i;

document.getElementById("showLabels").innerHTML = "<table><tr> <td width='120'>Field Name</td><td width='10'>&nbsp;</td><td width='70'>Required?</td><td width='10'>&nbsp;</td><td width='120'>Value</td><td width='10'>&nbsp;</td><td width='134'>Length</td><td width='10'>&nbsp;</td><td width='100'><div id='howlong1'></div></td></tr>";

document.getElementById(showQuantity).innerHTML = "<tr><td><input type='text' name='fieldtextname" + j + "' value='' size='20'></td><td width='10'>&nbsp;</td><td width='70'><select name='requiredtext" + j + "'><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='valuetext" + j + "' value='' size='20'></td><td width='10'>&nbsp;</td><td width='134'> <label> Empty <input type='radio' name='RadiotextGroup" + j + "' value='empty' onClick='hidelength(" + j + ")'>Length</label> <label><input type='radio' name='RadiotextGroup" + j + "' value='length' onClick='showlength(" + j + ")'></label></td><td width='10'>&nbsp;</td><td width='100'><div id ='lengthbox1'></div></td></tr></table>";

document.getElementById("textfieldbuildbutton").innerHTML = "<input type='button' name='build' value='Build code' onClick='javascript:textfieldbuildCode(formTool.fieldtextname1.value,formTool.fieldtextname3.value);'>";
}
}
}
}

I have the div tags later on down which the innerHTML goes into. I was wondering if there was a way to do like a foreach loop so the loop can just go through and gather all of the user input and put it into an array. If anyone can help I would greatly appreciate it sooo much. Thank you in advance!

Rambo Tribble

3:16 pm on Jul 29, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



See if this gives you any ideas:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Untitled</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<style type="text/css">
#main{height:4000px;}
</style>
<script type="text/javascript">
function fillPg(){
var pg_ht=document.getElementById("main").offsetHeight;
var cont_ht=new Array(200,300,200,300);
var cont_str=new Array("alpha","beta","delta","gamma");
var ttl_ht=0;
var wrt_str="";
var arr_ln=cont_str.length;
while(ttl_ht<pg_ht){
for(var i=0;i<arr_ln;i++){
wrt_str+="<div>"+cont_str[i]+"</div>";
ttl_ht+=cont_ht[i];
}
}
document.getElementById("main").innerHTML=wrt_str;
}
</script>
</head>
<body onload="fillPg();">
<div id="main">
</div>
</body>
</html>

treeleaf20

12:34 pm on Aug 2, 2004 (gmt 0)

10+ Year Member



That gives me an idea but I'm not sure how you could pass the variables to this function and then get them into the array. For example, on this line:

document.getElementById("textfieldbuildbutton").innerHTML = "<input type='button' name='build' value='Build code' onClick='javascript:textfieldbuildCode(formTool.fieldtextname1.value,formTool.fieldtextname3.value);'>";

The onClick command needs to pass it all of the parameters, I'm not sure how to do this since the amount of fields can vary....also I'm not sure how to get the variables once they have been passed. All help would be greatly appreciated and I thank you in advance.

treeleaf20

12:52 pm on Aug 2, 2004 (gmt 0)

10+ Year Member



I'm also trying this code but its giving me an error saying that wr_str is null. Here is the code if someone can help:

var cont_str = "<tr><td><input type='text' name='fieldtextname" + j + "' value='' size='20'></td><td width='10'>&nbsp;</td><td width='70'><select name='requiredtext" + j + "'><option value='Yes'>Yes</option><option value='No'>No</option></select></td><td width='10'>&nbsp;</td><td><input type='text' name='valuetext" + j + "' value='' size='20'></td><td width='10'>&nbsp;</td><td width='134'> <label> Empty <input type='radio' name='RadiotextGroup" + j + "' value='empty'>Length</label> <label><input type='radio' name='RadiotextGroup" + j + "' value='length'></label></td><td width='10'>&nbsp;</td><td><input type='text' name='length" + j + "' value='0' size='20' onClick='checklength()' disabled></td></tr></table>";

wrt_str+="<div>"+cont_str[j]+"</div>";

Bernard Marx

1:40 pm on Aug 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You'll have to excuse me if I have the wrong end of the stick.

From #3

document.getElementById("textfieldbuildbutton").innerHTML = ...

What type of element is the element id:textfieldbuildbutton?

Note that you don't need the javascript: protocol in an onclick event handler.

From #4

giving me an error saying that wr_str is null

That's because you are attempting to add (+=) to a variable that you haven't yet defined.
Defining: wrt_str = "" should solve that.

Overall, it might be better to do all this using DOM techniques, createElement etc, instead of building complicated strings, and having to mess with quoting. Using strings all the time makes it very difficult to create relationships between objects.

treeleaf20

2:05 pm on Aug 2, 2004 (gmt 0)

10+ Year Member



To reply to your questions...the textfieldbuildbutton element is just a div tag where it puts a button in which allows a user to build code based on things declared earlier. My next question is that I now have this code:

var wrt_str="";

var cont_str = new Array("<tr><td><input type='text' name='fieldtextname" + j + "' value='' size='20'></td><td width='10'>&nbsp;</td><td width='70'><select name='requiredtext" + j + "'><option value='Yes'>Yes</option><option value='No'>No</option></select></td><td width='10'>&nbsp;</td><td><input type='text' name='valuetext" + j + "' value='' size='20'></td><td width='10'>&nbsp;</td><td width='134'> <label> Empty <input type='radio' name='RadiotextGroup" + j + "' value='empty'>Length</label> <label><input type='radio' name='RadiotextGroup" + j + "' value='length'></label></td><td width='10'>&nbsp;</td><td><input type='text' name='length" + j + "' value='0' size='20' onClick='checklength()' disabled></td></tr></table>");

wrt_str+="<div>"+cont_str[j]+"</div>";

This code doesnt' give me any errors but it says that wrt_str is undefined when it prints it out to the page. I think its something with how I'm putting in the information into the cont_str array but I'm not sure how to really do that...I appreciate all of your help still...