Link to home
Start Free TrialLog in
Avatar of pgkooijman
pgkooijman

asked on

Preselecting a value in a dynamic selection box

Hi I have a selection box that is dynamically filled by choosing a value from another selection box. So for instance:

List of Continents
List of Countries
List of States

Now I am building a page that is about a country so I want the Continent and Countries box to be preselected and the States list to be filled with the values depending on the selected country.

How can I preselect an item from the countries list if it is dynamically created by the continents list. How can I in general select a value from a selection box?

Avatar of archrajan
archrajan

<select name = "myselect">
<option selected>option1</option>
<option>option2</option>
</select>

This is preselected
ASKER CERTIFIED SOLUTION
Avatar of archrajan
archrajan

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
see this example
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
<script>
function dynamicsel()
{
var a= document.myform
var opt = new Option("USA","USA","USA")
var opt1 = new Option("INDIA","INDIA","INDIA")
var opt2 = new Option("UK","UK","UK")
a.mysel.options[0] = opt1
a.mysel.options[1] = opt
a.mysel.options[2] = opt2
}
function dyninit()
{
var a = document.myform
for(i =0; i<a.mysel.length; i++)
{
if(a.mysel.options[i].value == "USA")
a.mysel.options[i].selected = true;
}
}
</script>


</HEAD>

<BODY onload = "dynamicsel();dyninit();">
<form name = "myform">
<select name = "mysel">
</select>
</BODY>
</HTML>
Avatar of devic
another simple way without to modifiy other script:

<script>
document.formName.selectBoxName.selectedIndex=5;
</script>