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 2nd part

Status
Not open for further replies.

emozley

Technical User
Jan 14, 2003
769
GB
Hi,

I've done a bit of research and it seems there are two extra boolean values you can use when creating options on a dropdown menu. The 3rd one being whether it is the default selection and the 4th is whether it is selected or not.

I thought by populating my array with these values I would be able to specify which one would be selected. so for example if the US is selected Los Angelese is the default option, Canada would give Toronta etc. The problem is it always selects the last item in the list by default - where am I going wrong?

Thanks

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" SELECTED>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|false|false", "Los Angeles|loangelesvalue|false|true", "Chicago|chicagovalue|false|false", "Houston|houstonvalue|false|false", "Austin|austinvalue|false|false"]
Stages[2]=["Vancouver|vancouvervalue|false|false", "Tonronto|torontovalue|false|true", "Montreal|montrealvalue|false|false", "Calgary|calgaryvalue|false|false"]
Stages[3]=["London|londonvalue|false|false", "Glasgow|glasgowsvalue|false|true", "Manchester|manchestervalue|false|false", "Edinburgh|edinburghvalue|false|false", "Birmingham|birminghamvalue|false|false"]

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], Stages[SelectedStageGroup].split("|")[2], Stages[SelectedStageGroup].split("|")[3])
}
}

</script>
 
Hmmm... the problem with the default selection value is that it refers to whether the option was declared in the HTML with the [tt]selected[/tt] attribute.

You might be better off to do this in two stages. One loop that runs through and creates all the options. And another that runs through and selects the appropriate default option.

Never be afraid to share your dreams with the world.
There's nothing the world loves more than the taste of really sweet dreams.

Enable Apps
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top