Click to See Complete Forum and Search --> : Problem Selecting Option in ListBox


kateyez44
11-22-2002, 09:51 AM
Hi. I am pretty new to JavaScript and am having some difficulties. The project I'm working on incorporates code that was written by our client. The piece I'm having a problem with pre-populates a Select box on an ASP page with a list of months.

In some instances, one of the months in the select box will need to be pre-selected. This is where the problem lies. I cannot figure out how to get the Select box to pre-select anything. Here is the code I have been trying:


<!--
//this is the function that pre-populates the combo
document.write(populateMonths());
//this is where I'm trying to pre-select a specific value
document.frmClaimantDrug.lstEmpBeginM.SelectedIndex = "<%=intMonth%>"

//I have also tried:
document.frmClaimantDrug.lstEmpBeginM.Value = "<%strMonth%>"
// -->


The value for intMonth is obtained through a VBScript function I use in the ASP page. I have tested this and I know that this variable contains the correct value. Please tell me what I am doing wrong. This is driving me mad. Thank you.

Rodders
11-22-2002, 10:14 AM
The selectedIndex property must be a valid index of the SELECT OPTIONS array. What this means is, the options you have specified for your SELECT each have a unique index starting at 0 and incrementing by 1 for each option. So the first option in the list can be set by saying

document.formname.selectname.selectedIndex = 0;

and the second by.....

document.formname.selectname.selectedIndex = 1;


What you need to do is work out which index refers to which month. You could just save the index to your database so that 0 is january and 11 is december. Then your SELECT could look like this...

<SELECT NAME=mySelect>
<OPTION VALUE=0>January</OPTION>
<OPTION VALUE=1>February</OPTION>
....
....
<OPTION VALUE=11>December</OPTION>
</SELECT>

When you submit this the value of mySelect will be set to the value of the selected option which you can save direct to the database.

Make sense?