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

Insert JS in page

Status
Not open for further replies.

JimJx

Technical User
Feb 16, 2001
202
US
Hi all,

I have what is probably a simple problem but since I have been away from JS for a whiule it escapes me....

I have a form that needs a select box. The options in this select are incremental, such as FORM1 FORM2 ... FORM100

Instead of writing out each option, can I use some JS to loop through 1 - 100 and write them out when the page loads?

Any info greatly appreciated.
Jim
 
yes, but the preferred method is server-side code.

try something like this:

Code:
var s = document.forms['formname'].elements['selectname'];
s.options.length = 0;
var totalOptions = 100;

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

i didn't test it, but it should do the trick.

an alternative method is to build an innerHTML string and then set the innerHTML of the select at the end of the loop.



*cLFlaVA
----------------------------
[tt]mr. pibb + red vines = crazy delicious![/tt]

[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top