Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

For Loop and value of l

Status
Not open for further replies.

bobbybrown69

Programmer
Jun 11, 2003
15
US
How would I go about allowing oNewTd4 be whatever l+1 is equal to in this for loop. I have it hardcoded as oNewTd4 and oNewTd4 but sometimes it might need to be 6 or 8 or 11, etc.

I thought this would work but it didn't.
eval("oNewTd"+(l+1));
--------------------------------

for(var l=0, field, elm; field=lblFieldOrder[l++];) {
oNewTd = "";
if ((field == "Year4") || (field == "Year2")){
if (linerYear4 != ""){
oNewTd = eval("oNewTd"+(l+1));
oNewTd4 = document.createElement("td");
}
if (linerYear2 != ""){
oNewTd = eval("oNewTd"+(l+1));
oNewTd4 = document.createElement("td");
}
}
if (field == "Model"){
oNewTd = eval("oNewTd"+(l+1));
oNewTd3 = document.createElement("td");
}
}
 
If you cannot explain what you want with clarity, chances are you would not script it right and anybody else for you would have even lesser chance. (if l=0 what happens, l=1 what happens,...)

As to the structural part.
[tt]
for(var l=0, field, elm; field=lblFieldOrder[l++];) {
oNewTd = "";
switch (field) {
case "Year4" :
case "Year2" :
if (linerYear4 != "" || linerYear2 != ""){
oNewTd = eval("oNewTd"+(l+1));
oNewTd4 = document.createElement("td");
} else {
//what happens? and what set?
}
break;
case "Model" :
oNewTd = eval("oNewTd"+(l+1));
oNewTd3 = document.createElement("td"); //oNewTd3???
break;
default :
//do nothing ?
}
}
[/tt]
 
>How would I go about allowing oNewTd4 be whatever l+1 is equal to in this for loop. I have it hardcoded as oNewTd4 and oNewTd4 but sometimes it might need to be 6 or 8 or 11, etc.

Upon re-reading it, this can disperse your doubt.
[tt]
eval("oNewTd"+l+1+"=document.createElement('td');");
[/tt]
But then, to avoid using eval(), you fall back to the array method in your previous thread! which is a better practice.
[tt]
someArray[l+1]=document.createElement("td"); //l>1
[/tt]
 
How would I go about allowing oNewTd4 be whatever l+1 is equal to in this for loop.

Given that l is a simple integer type, l+1 would also be a simple integer type.

So what you have told us is that you want to assign a simple integer type to variable "oNewTd4". This being the case, I do not understand why you are using eval. Use simply:

Code:
oNewTd4 = l + 1;

Of course, if that's not what you want, you really should try and explain a bit better what you do want.

Hope this helps,
Dan

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top