Click to See Complete Forum and Search --> : Need help shortening some code


rjusa
11-19-2003, 03:01 PM
I have the following type of code (I shortened it to 3 of 7 instances to take up less space):

function update() {
var output1 = '';
var output2 = '';
.
.
.
var output7 = '';

if (document.forms[0].selectfield.options[0].selected) {
output1 += document.forms[0].selectfield.options[0].text + ' '; }
if (document.forms[0].selectfield.options[1].selected) {
output2 += document.forms[0].selectfield.options[1].text + ' '; }
.
.
.
if (document.forms[0].selectfield.options[6].selected) {
output7 += document.forms[0].selectfield.options[6].text + ' '; }


opener.document.forms[0].resultfield1.value = output1;
opener.document.forms[0].resultfield2.value = output2;
.
.
.
opener.document.forms[0].resultfield7.value = output7;

Can someone please help shortening this creature...do you use something like 'for (i=0; i<7; i++)' I'm a bit confused.

Thanks,
Ron

gil davis
11-19-2003, 04:08 PM
function update() {
var output = new Array("","","","","","",""); //0-6
for (var i=0; i<7; i++)
{if (document.forms[0].selectfield.options[i].selected)
{output[i] += document.forms[0].selectfield.options[i].text + ' ';}
opener.document.forms[0]["resultfield" + i + 1].value = output[i];
}
It could have been a bit simpler to name all 7 fields the same and then there would be an array of them.
opener.document.forms[0].resultfield[i].value = output[i];No big deal.