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?

pgkooijmanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

This is preselected
0
archrajanCommented:
when the page loads dynamically u can have a javascript like this

function initselect()
{
 for (var i=0;i<selectboxname.options.length;i++){
    if (selectboxname.options[i].value=='theoptionvalue_which_u_want_selected')
      selectboxname.selectedIndex = i;
     break;
    }
}

and u can call the initselectfunction
in <body onload = "initselect()">
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
archrajanCommented:
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>
0
devicCommented:
another simple way without to modifiy other script:

<script>
document.formName.selectBoxName.selectedIndex=5;
</script>
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.