...

View Full Version : Populate form fields using a for loop



Martin46766474
04-10-2003, 04:37 PM
I managed to get this far with it but I'm struggling with the last two lines. Here's the bit of code I'm working on:




if (data != "") {
var fields=data.split("\r" )
for(var i=1; i<6; i++) {
var desturl = fields[i].substring(16,fields[i].indexOf("@"));
var lnktext = fields[i].substring(fields[i].indexOf("@")+1,fields[i].length-1);
document.myform.link1url.value=desturl;
document.myform.link1desc.value=lnktext;
}
}




As it is, link1url & link1desc get the info each time through the loop but the form fields are:

link1url & link1desc
link2url & link2desc
link3url & link3desc
link4url & link4desc
link5url & link5desc

So, can I replace the number in the middle of the field name with i each time it loops?

beetle
04-10-2003, 04:41 PM
I suspect you want something like this
var elems = document.myform.elements;
if (data != "") {
var fields=data.split("\r" )
for(var i=1; i<6; i++) {
var desturl = fields[i].substring(16,fields[i].indexOf("@"));
var lnktext = fields[i].substring(fields[i].indexOf("@")+1,fields[i].length-1);
elems['link' + i + 'url'].value=desturl;
elems['link' + i + 'desc'].value=lnktext;
}
}

Martin46766474
04-10-2003, 04:59 PM
Cheers, beetle - that works great.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum