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?
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?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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","IN DIA")
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].valu e == "USA")
a.mysel.options[i].selecte d = true;
}
}
</script>
</HEAD>
<BODY onload = "dynamicsel();dyninit();">
<form name = "myform">
<select name = "mysel">
</select>
</BODY>
</HTML>
<!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","IN
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].valu
a.mysel.options[i].selecte
}
}
</script>
</HEAD>
<BODY onload = "dynamicsel();dyninit();">
<form name = "myform">
<select name = "mysel">
</select>
</BODY>
</HTML>
another simple way without to modifiy other script:
<script>
document.formName.selectBo xName.sele ctedIndex= 5;
</script>
<script>
document.formName.selectBo
</script>
<option selected>option1</option>
<option>option2</option>
</select>
This is preselected