/    Sign up×
Community /Pin to ProfileBookmark

Problem Selecting Option in ListBox

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:

[i][b]
<!–
//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%>”
// –>
[/i]
[/b]

The value for [b]intMonth[/b] 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.

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@RoddersNov 22.2002 — 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?
×

Success!

Help @kateyez44 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.5,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...