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

How can i set the option values of a dropdown list at onload? 2

Status
Not open for further replies.

Petal

Programmer
Jun 8, 2001
56
NO
Hi
I would like to set the values of the options in a dropdown list with a javascript when the page loads. I have tried with:

<script language=&quot;JavaScript&quot;>
function ini(){
dimg80 = new Array(2)
dimg80[0] =new Array(&quot;6 mm&quot;,&quot;8 mm&quot;,&quot;10 mm&quot;,&quot;12 mm&quot;,&quot;14 mm&quot;,&quot;16 mm&quot;,&quot;20 mm&quot;,&quot;24 mm&quot;,&quot;30 mm&quot;,&quot;36 mm&quot;,&quot;42 mm&quot;,&quot;48 mm&quot;)
dimg80[1] =new Array(0.1,0.2,0.25,0.4,0.75,1,1.5,2,3,4,6,8)

for (var i = 0; i < dimg80[1].length; i++) {
dimensjon.options=new Option(dimg80[0],dimg80[1]);
}
}
</javascript>
<body onload=&quot;ini()>
<form>
<select name=&quot;dimensjon>
</select>
</form>
</body>


I got this error: 'dimensjon' is undefined. I think this happend because the script is loaded before the hole page are loaded. How can i set the values for dimensjon.option[] with a javascript onload?

Jørn Arild Andenæs
jaa@jaa.no
 
there are a few syntax errors in the code, try the following:

<html><head>
<script language=&quot;JavaScript&quot;>
function ini(){
dimg80 = new Array(2)
dimg80[0] =new Array(&quot;6 mm&quot;,&quot;8 mm&quot;,&quot;10 mm&quot;,&quot;12 mm&quot;,&quot;14 mm&quot;,&quot;16 mm&quot;,&quot;20 mm&quot;,&quot;24 mm&quot;,&quot;30 mm&quot;,&quot;36 mm&quot;,&quot;42 mm&quot;,&quot;48 mm&quot;)
dimg80[1] =new Array(0.1,0.2,0.25,0.4,0.75,1,1.5,2,3,4,6,8)
for (var i = 0; i < dimg80[1].length; i++) {
document.forms[0].dimensjon.options[document.forms[0].dimensjon.options.length]=new Option(dimg80[0],dimg80[1]);
}
}
</script>
</head>
<body onload=&quot;ini()&quot;>
<form>
<select name=&quot;dimensjon&quot;>
</select>
</form>
</body>
</html>
 
2 soluotions :

1.) call the code after the closing form tag eg :

Code:
<script>
<!--
ini ();
//-->

2.) use setTimeout eg :

Code:
<body onload=&quot;setTimeout ('ini ();', 2000);&quot;>
Regards

David Byng

spider.gif


davidbyng@hotmail.com
 
Actually I think this is what you want :

Code:
<html>
  <head>
    <script language=&quot;JavaScript&quot;>
    <!--
        function ini (field) {
            dimg800 = new Array (&quot;6 mm&quot;,&quot;8 mm&quot;,&quot;10 mm&quot;,&quot;12 mm&quot;,&quot;14 mm&quot;,&quot;16 mm&quot;,&quot;20 mm&quot;,&quot;24 mm&quot;,&quot;30 mm&quot;,&quot;36 mm&quot;,&quot;42 mm&quot;,&quot;48 mm&quot;)
            dimg801 = new Array (0.1,0.2,0.25,0.4,0.75,1,1.5,2,3,4,6,8)
            for (var i = 0; i < dimg801.length; i++) {
                document.forms[0][field].options[document.forms[0].dimensjon.options.length] = new Option (dimg800[i],dimg801[i]);
            }
        }
    //-->
    </script>
  </head>
  <body onload=&quot;ini('dimensjon')&quot;>
    <form>
      <select name=&quot;dimensjon&quot;>
      </select>
    </form>
  </body>
</html>
Regards

David Byng

spider.gif


davidbyng@hotmail.com
 
Thanks to you both!

Specially BigBadDave saved my day! :) Jørn Arild Andenæs
jaa@jaa.no
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top