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

Item selected by default on dropdown

Status
Not open for further replies.

emozley

Technical User
Jan 14, 2003
769
GB
Hi,

I have a downloaded a small script which builds two interdependent menus - ie selecting an item from the first list changes what appears on the 2nd list. I have adapted it so the options available are pulled from a database.

If I want UK to be selected by default on the first list of course that is very easy but is it possible to change this so Manchester was selected by default on the 2nd list when the page loads?

Thanks very much

Ed

<form name="addtmform">
<select name="Area" size="4" onChange="UpdateStages(this.selectedIndex)" style="width: 150px">
<option selected>Select A City</option>
<option value="usa">USA</option>
<option value="canada">Canada</option>
<option value="uk">United Kingdom</option>
</select>

<select name="Stages" size="4" style="width: 150px" onClick="alert(this.options[this.options.selectedIndex].value)">
</select>
</form>

<script type="text/javascript">

var Arealist=document.addtmform.Area
var Stageslist=document.addtmform.Stages

var Stages=new Array()
Stages[0]=""
Stages[1]=["New York|newyorkvalue", "Los Angeles|loangelesvalue", "Chicago|chicagovalue", "Houston|houstonvalue", "Austin|austinvalue"]
Stages[2]=["Vancouver|vancouvervalue", "Tonronto|torontovalue", "Montreal|montrealvalue", "Calgary|calgaryvalue"]
Stages[3]=["London|londonvalue", "Glasgow|glasgowsvalue", "Manchester|manchestervalue", "Edinburgh|edinburghvalue", "Birmingham|birminghamvalue"]

function UpdateStages(SelectedStageGroup){
Stageslist.options.length=0
if (SelectedStageGroup>0){
for (i=0; i<Stages[SelectedStageGroup].length; i++)
Stageslist.options[Stageslist.options.length]=new Option(Stages[SelectedStageGroup].split("|")[0], Stages[SelectedStageGroup].split("|")[1])
}
}

</script>

 
There are 2 things you can do to make this happen.

1. Build the default value into the Option command.

2. Set the value of the dropdown to "manchestervalue" after you build the DHTML dropdown.


Method 1
Code:
Stageslist.options[Stageslist.options.length]=new Option(Stages[SelectedStageGroup][i].split("|")[0], Stages[SelectedStageGroup][i].split("|")[1][!], true, true[/!])

Method 2
Code:
for (i=0; i<Stages[SelectedStageGroup].length; i++) {
   Stageslist.options[Stageslist.options.length]=new Option(Stages[SelectedStageGroup][i].split("|")[0], Stages[SelectedStageGroup][i].split("|")[1])
}
StagesList.value =  "manchestervalue";

[monkey][snake] <.
 
Yeah my method one will not work. You have to check to see if the value is = "manchestervalue", THEN write the code I have in Method 1, else don't add the booleans.



Code:
if (Stages[SelectedStageGroup][i].split("|")[1] == "manchestervalue") {
   Stageslist.options[Stageslist.options.length]=new Option(Stages[SelectedStageGroup][i].split("|")[0], Stages[SelectedStageGroup][i].split("|")[1], true, true);
}
else {
   Stageslist.options[Stageslist.options.length]=new Option(Stages[SelectedStageGroup][i].split("|")[0], Stages[SelectedStageGroup][i].split("|")[1]);
}

There, that will work. I'm being careless.


[monkey][snake] <.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top