Leo Leo - 4 years ago 89
HTML Question

Drop down menu for selecting country then states

I want a to create a drop down menu which generates another drop down menu on the base of selected value
Just for example if select United States from the 1st drop down then 2nd drop down contains the states of United states.
I've done much of the job. I am prefering a javascript and using arrays

var country_arr = new Array("USA", "Singapore", "Pakistan")
var s_a = new Array();

But I also want the "name" of the country and state selected, for sending to mysql database.
I guess i've to use double dimensional array for this. But unable to code this time and seriously need your help.

Answer Source
<!DOCTYPE html>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <script type="text/javascript">
        var citiesByState = {
            USA: ["NY","NJ"],
            Singapore: ["taas","naas"]
        function makeSubmenu(value) {
            if(value.length==0) document.getElementById("citySelect").innerHTML = "<option></option>";
            else {
                var citiesOptions = "";
                for(cityId in citiesByState[value]) {
                document.getElementById("citySelect").innerHTML = citiesOptions;
        function displaySelected() {
            var country = document.getElementById("countrySelect").value;
            var city = document.getElementById("citySelect").value;
        function resetSelection() {
            document.getElementById("countrySelect").selectedIndex = 0;
            document.getElementById("citySelect").selectedIndex = 0;
<body onload="resetSelection()">
    <select id="countrySelect" size="1" onchange="makeSubmenu(this.value)">
    <select id="citySelect" size="1">
    <button onclick="displaySelected()">show selected</button>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download