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 Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

looping through selects

Status
Not open for further replies.

tiamat2012

Programmer
Dec 12, 2004
144
US
Hey all,

I was wondering if there were any way to loop through selects? It looks really sloppy to write out 12 different assignments, here's my code:

Code:
					  		document.MyForm.pic1.options[i] = new Option(picarray[i], picarray[i]);
					  		document.MyForm.pic2.options[i] = new Option(picarray[i], picarray[i]);
					  		document.MyForm.pic3.options[i] = new Option(picarray[i], picarray[i]);
					  		document.MyForm.pic4.options[i] = new Option(picarray[i], picarray[i]);
					  		document.MyForm.pic5.options[i] = new Option(picarray[i], picarray[i]);
					  		document.MyForm.pic6.options[i] = new Option(picarray[i], picarray[i]);
					  		document.MyForm.pic7.options[i] = new Option(picarray[i], picarray[i]);
					  		document.MyForm.pic8.options[i] = new Option(picarray[i], picarray[i]);
					  		document.MyForm.pic9.options[i] = new Option(picarray[i], picarray[i]);
					  		document.MyForm.pic10.options[i] = new Option(picarray[i], picarray[i]);
					  		document.MyForm.pic11.options[i] = new Option(picarray[i], picarray[i]);
					  		document.MyForm.pic12.options[i] = new Option(picarray[i], picarray[i]);

I would like to do a loop through the pic1, pic2, etc. (which are <selects>)

anyone know how I could do that?

something like document.MyForm.select wouldn't work because I have many more selects than just these on the page, unless I were to find which number select it was.

Thank you,
-Kerry
 
Like this.
[tt]
for (var i=1;i<=12;i++) {
document.MyForm.elements["pic"+i].options = new Option(picarray, picarray);
}
[/tt]
 
Correction

My mixed up of indices. A re-take.
[tt]
//i given from another loop, for instance
for (var [red]j[/red]=1;[red]j[/red]<=12;[red]j[/red]++) {
document.MyForm.elements["pic"+[red]j[/red]].options = new Option(picarray, picarray);
}
[/tt]
 
here's another method, based on your code above:

Code:
var sColl = document.forms['myForm'].getElementsByTagName("select");

for ( var s = 0; s < sColl.length; s++ ) {
    sColl[s].options[i] = new Option( picarray[i], picarray[i] );
}



*cLFlaVA
----------------------------
[tt]( <P> <B>)13 * (<P> <.</B>)[/tt]

[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
Thank you!

tsuji I used your method, but tweaked it with the eval function. I did this
Code:
for(var j = 1; j <= 12; j++) {
rselobject[j] = "document.MyForm.pic" + j;			
}

then

Code:
for (var i = 0; i < piclength-1; i++) {
  for(var k = 1; k <=12; k++) {
    eval(rselobject[k]).options[i] = new Option(picarray[i], picarray[i]);
  }
}

I had to use the rselobject more than once which is why I set it as a variable.

and thank you cLFlaVA, only problem was I had about 20 other selects in my form that I didn't want to hit (which is why I didn't use that method).

Thank you,
Kerry
 
This is an admin section of a website, speed is not required and runs perfectly fine for slow operations, but I'll definitely avoid it in the future

why should it just plain not be used? and is there another method?

-Kerry
 
of course, you can do this:

Code:
var e = document.forms['myForm'].elements;
for (var i = 0; i < piclength-1; i++) {
  for(var k = 1; k <=12; k++) {
    e['pic' + k].options[i] = new Option(picarray[i], picarray[i]);
  }
}

why should it just plain not be used?

didn't i give you three reasons above?

it's antiquated, slow and should just plain not be used.



*cLFlaVA
----------------------------
[tt]( <P> <B>)13 * (<P> <.</B>)[/tt]

[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
Ok cool, I'll try that.

If you say those are the three, then I queried the third... word for word.

and should "just plain not be used".
why should it "just plain not be used"?

Thanks,
Kerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top