Preselecting a value in a dynamic selection box

Posted on 2004-11-24
Last Modified: 2011-10-03
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?

Question by:pgkooijman
    LVL 25

    Expert Comment

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

    This is preselected
    LVL 25

    Accepted Solution

    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;

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

    Expert Comment

    see this example
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <TITLE> New Document </TITLE>
    <META NAME="Generator" CONTENT="EditPlus">
    <META NAME="Author" CONTENT="">
    <META NAME="Keywords" CONTENT="">
    <META NAME="Description" CONTENT="">
    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;


    <BODY onload = "dynamicsel();dyninit();">
    <form name = "myform">
    <select name = "mysel">
    LVL 25

    Expert Comment

    another simple way without to modifiy other script:


    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (, typical array handling might look like this: (CODE) B…
    International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now