• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 153
  • Last Modified:

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?

0
pgkooijman
Asked:
pgkooijman
  • 3
1 Solution
 
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
 
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now