Forum Moderators: open

Message Too Old, No Replies

Adding to var && for loop

         

scoobydoo987

7:18 pm on Oct 18, 2005 (gmt 0)

10+ Year Member



I need to figure out how to add l+1 to the end of oNewTd. Right now I have it hard coded as i.e. oNewTd6 but I need it to know the value of l+1 and add it to oNewTd

So if l is = 5 then oNewTd would = oNewTd6
-----------------------------

for(var l=0, field, elm; field=lblFieldOrder[l++];) {
if ((field == "Year4") ¦¦ (field == "Year2")){
oNewTd6.innerHTML = "<input type=text size=2 style='background-color: #FFFFFF; visibility= ;' id='linerYear4' onfocus=showPreview('" + vid + "') onblur=hidePreview('" + vid + "') onpropertychange=updateLine('" + vid + "') class=copy name=liner_Year4_" + vid + " value=\"" + trim(linerYear4) + "\"><input type=text size=2 style='background-color: #FFFFFF; visibility: ;' id='linerYear2' onfocus=showPreview('" + vid + "') onblur=hidePreview('" + vid + "') onpropertychange=updateLine('" + vid + "') class=copy name=liner_Year2_" + vid + " value=\"" + trim(linerYear2) + "\">";
}
if(field == "Make"){
oNewTd2.innerHTML = "<div id='linerMake' style='visibility: ; '><input type=text size=15 style='background-color: #FFFFFF' id='linerMake' onfocus=showPreview('" + vid + "') onblur=hidePreview('" + vid + "') onpropertychange=updateLine('" + vid + "') class=copy name=liner_Make_" + vid + " value=\"" + trim(linerMake) + "\"></div>";
}
if(field == "Model"){
oNewTd3.innerHTML = "<div id='linerModel' style='visibility: ; '><input type=text size=15 style='background-color: #FFFFFF' id='linerModel' onfocus=showPreview('" + vid + "') onblur=hidePreview('" + vid + "') onpropertychange=updateLine('" + vid + "') class=copy name=liner_Model_" + vid + " value=\"" + trim(linerModel) + "\"></div>";
}
}

garann

9:30 pm on Oct 18, 2005 (gmt 0)

10+ Year Member



Is
oNewTd6
a variable? If so, you might have an easier time putting the elements
"oNewTd"+(l+1)
can possibly represent in an array, and using the
l
variable to get the one you want. Either that or I'm totally missing what you're saying...

scoobydoo987

10:12 pm on Oct 18, 2005 (gmt 0)

10+ Year Member



It works fine if I hardcode a 2 after oNewTd in the commented code below but I need the value of l+1 to be at the end of oNewTd

I tried this but it didn't work.
oNewTd = "oNewTd"+(l+1);
"+oNewTd+".innerHTML = "<div id='linerMake' style='visibility: ; '><input type=text size=15 style='background-color: #FFFFFF' id='linerMake' onfocus=showPreview('" + vid + "') onblur=hidePreview('" + vid + "') onpropertychange=updateLine('" + vid + "') class=copy name=liner_Make_" + vid + " value=\"" + trim(linerMake) + "\"></div>";

-------------------------
//oNewTd2.innerHTML = "<div id='linerMake' style='visibility: ; '><input type=text size=15 style='background-color: #FFFFFF' id='linerMake' onfocus=showPreview('" + vid + "') onblur=hidePreview('" + vid + "') onpropertychange=updateLine('" + vid + "') class=copy name=liner_Make_" + vid + " value=\"" + trim(linerMake) + "\"></div>";

garann

10:40 pm on Oct 18, 2005 (gmt 0)

10+ Year Member



Yeah, that shouldn't work. You're trying to dynamically get the name of a variable by adding a String to it. So what you're actually doing is changing
oNewTd
from an element (I assume?) to a String. You can, as I said above, put all the elements
oNewTd + "whatever"
should represent in an array and access them that way. Or, if you have some bias against arrays, you can use the rather inelegant eval method like so:
oNewTd = eval("oNewTd"+(l+1))

scoobydoo987

1:09 am on Oct 19, 2005 (gmt 0)

10+ Year Member



The eval worked but since I am new to JavaScript what would be the reason to use an array over the eval?

Would it speed things up?

garann

11:04 pm on Oct 25, 2005 (gmt 0)

10+ Year Member



It should be a little quicker, since you'll already have a reference to the element, rather than having to create it on the fly each time you want it.