Dynamic Country State Javascript Menu Example


I have found myself in a couple of situations where I needed a simple dynamic javascript menu where the user would make a selection from one select list and depending on what the user selected from the first select list a second list would offer further options. Here is a perfect example:

Demo

Country:

State/Province:

Country value:

 

State/Province value:
 

In this example, the state/province menu is disabled until the user has first selected a country. Then depending on the country that is chosen, the list of associated states or provinces would follow. For the sake of simplicity, only the state/province menus have been implemented for the United States and Canada. If any other country is chosen, the value for the state input defaults to Other.

Here is the HTML code for the example:

<script src="/path to JavaScript File/countryToStateMenu.js" language="Javascript" type="text/javascript"></script>
<p>Country:</p>
<select name="country" id="country" onChange="printStateMenu(this.value);">'.
     '<option>Select country...</option>'.
     '<option value="US">United States of America</option>'.
     '<option value="CA">Canada</option>'.
     '<option value="AF">Afghanistan</option>'.
     '<option value="AX">Åland</option>'.
     '<option value="AL">Albania</option>'.
     '<option value="DZ">Algeria</option>'.
     '<option value="AS">American Samoa</option>'.
     '<option value="AD">Andorra</option>'.
     '<option value="AO">Angola</option>'.
     '<option value="AI">Anguilla</option>'.
     '<option value="AQ">Antarctica</option>'.
     '<option value="AG">Antigua and Barbuda</option>'.
     '<option value="AR">Argentina</option>'.
     '<option value="AM">Armenia</option>'.
     '<option value="AW">Aruba</option>'.
     '<option value="AU">Australia</option>'.
     '<option value="AT">Austria</option>'.
     '<option value="AZ">Azerbaijan</option>'.
     '<option value="BS">Bahamas</option>'.
     '<option value="BH">Bahrain</option>'.
     '<option value="BD">Bangladesh</option>'.
     '<option value="BB">Barbados</option>'.
     '<option value="BY">Belarus</option>'.
     '<option value="BE">Belgium</option>'.
     '<option value="BZ">Belize</option>'.
     '<option value="BJ">Benin</option>'.
     '<option value="BM">Bermuda</option>'.
     '<option value="BT">Bhutan</option>'.
     '<option value="BO">Bolivia</option>'.
     '<option value="BA">Bosnia and Herzegovina</option>'.
     '<option value="BW">Botswana</option>'.
     '<option value="BV">Bouvet Island</option>'.
     '<option value="BR">Brazil</option>'.
     '<option value="IO">British Indian Ocean Territory</option>'.
     '<option value="BN">Brunei Darussalam</option>'.
     '<option value="BG">Bulgaria</option>'.
     '<option value="BF">Burkina Faso</option>'.
     '<option value="BI">Burundi</option>'.
     '<option value="KH">Cambodia</option>'.
     '<option value="CM">Cameroon</option>'.
     '<option value="CV">Cape Verde</option>'.
     '<option value="KY">Cayman Islands</option>'.
     '<option value="CF">Central African Republic</option>'.
     '<option value="TD">Chad</option>'.
     '<option value="CL">Chile</option>'.
     '<option value="CN">China</option>'.
     '<option value="CX">Christmas Island</option>'.
     '<option value="CC">Cocos (Keeling) Islands</option>'.
     '<option value="CO">Colombia</option>'.
     '<option value="KM">Comoros</option>'.
     '<option value="CG">Congo (Brazzaville)</option>'.
     '<option value="CD">Congo (Kinshasa)</option>'.
     '<option value="CK">Cook Islands</option>'.
     '<option value="CR">Costa Rica</option>'.
     '<option value="CI">Côte dIvoire</option>'.
     '<option value="HR">Croatia</option>'.
     '<option value="CU">Cuba</option>'.
     '<option value="CY">Cyprus</option>'.
     '<option value="CZ">Czech Republic</option>'.
     '<option value="DK">Denmark</option>'.
     '<option value="DJ">Djibouti</option>'.
     '<option value="DM">Dominica</option>'.
     '<option value="DO">Dominican Republic</option>'.
     '<option value="EC">Ecuador</option>'.
     '<option value="EG">Egypt</option>'.
     '<option value="SV">El Salvador</option>'.
     '<option value="GQ">Equatorial Guinea</option>'.
     '<option value="ER">Eritrea</option>'.
     '<option value="EE">Estonia</option>'.
     '<option value="ET">Ethiopia</option>'.
     '<option value="FK">Falkland Islands</option>'.
     '<option value="FO">Faroe Islands</option>'.
     '<option value="FJ">Fiji</option>'.
     '<option value="FI">Finland</option>'.
     '<option value="FR">France</option>'.
     '<option value="GF">French Guiana</option>'.
     '<option value="PF">French Polynesia</option>'.
     '<option value="TF">French Southern Lands</option>'.
     '<option value="GA">Gabon</option>'.
     '<option value="GM">Gambia</option>'.
     '<option value="GE">Georgia</option>'.
     '<option value="DE">Germany</option>'.
     '<option value="GH">Ghana</option>'.
     '<option value="GI">Gibraltar</option>'.
     '<option value="GR">Greece</option>'.
     '<option value="GL">Greenland</option>'.
     '<option value="GD">Grenada</option>'.
     '<option value="GP">Guadeloupe</option>'.
     '<option value="GU">Guam</option>'.
     '<option value="GT">Guatemala</option>'.
     '<option value="GG">Guernsey</option>'.
     '<option value="GN">Guinea</option>'.
     '<option value="GW">Guinea-Bissau</option>'.
     '<option value="GY">Guyana</option>'.
     '<option value="HT">Haiti</option>'.
     '<option value="HM">Heard and McDonald Islands</option>'.
     '<option value="HN">Honduras</option>'.
     '<option value="HK">Hong Kong</option>'.
     '<option value="HU">Hungary</option>'.
     '<option value="IS">Iceland</option>'.
     '<option value="IN">India</option>'.
     '<option value="ID">Indonesia</option>'.
     '<option value="IR">Iran</option>'.
     '<option value="IQ">Iraq</option>'.
     '<option value="IE">Ireland</option>'.
     '<option value="IM">Isle of Man</option>'.
     '<option value="IL">Israel</option>'.
     '<option value="IT">Italy</option>'.
     '<option value="JM">Jamaica</option>'.
     '<option value="JP">Japan</option>'.
     '<option value="JE">Jersey</option>'.
     '<option value="JO">Jordan</option>'.
     '<option value="KZ">Kazakhstan</option>'.
     '<option value="KE">Kenya</option>'.
     '<option value="KI">Kiribati</option>'.
     '<option value="KP">Korea ( North )</option>'.
     '<option value="KR">Korea ( South )</option>'.
     '<option value="KW">Kuwait</option>'.
     '<option value="KG">Kyrgyzstan</option>'.
     '<option value="LA">Laos</option>'.
     '<option value="LV">Latvia</option>'.
     '<option value="LB">Lebanon</option>'.
     '<option value="LS">Lesotho</option>'.
     '<option value="LR">Liberia</option>'.
     '<option value="LY">Libya</option>'.
     '<option value="LI">Liechtenstein</option>'.
     '<option value="LT">Lithuania</option>'.
     '<option value="LU">Luxembourg</option>'.
     '<option value="MO">Macau</option>'.
     '<option value="MK">Macedonia</option>'.
     '<option value="MG">Madagascar</option>'.
     '<option value="MW">Malawi</option>'.
     '<option value="MY">Malaysia</option>'.
     '<option value="MV">Maldives</option>'.
     '<option value="ML">Mali</option>'.
     '<option value="MT">Malta</option>'.
     '<option value="MH">Marshall Islands</option>'.
     '<option value="MQ">Martinique</option>'.
     '<option value="MR">Mauritania</option>'.
     '<option value="MU">Mauritius</option>'.
     '<option value="YT">Mayotte</option>'.
     '<option value="MX">Mexico</option>'.
     '<option value="FM">Micronesia</option>'.
     '<option value="MD">Moldova</option>'.
     '<option value="MC">Monaco</option>'.
     '<option value="MN">Mongolia</option>'.
     '<option value="ME">Montenegro</option>'.
     '<option value="MS">Montserrat</option>'.
     '<option value="MA">Morocco</option>'.
     '<option value="MZ">Mozambique</option>'.
     '<option value="MM">Myanmar</option>'.
     '<option value="NA">Namibia</option>'.
     '<option value="NR">Nauru</option>'.
     '<option value="NP">Nepal</option>'.
     '<option value="NL">Netherlands</option>'.
     '<option value="AN">Netherlands Antilles</option>'.
     '<option value="NC">New Caledonia</option>'.
     '<option value="NZ">New Zealand</option>'.
     '<option value="NI">Nicaragua</option>'.
     '<option value="NE">Niger</option>'.
     '<option value="NG">Nigeria</option>'.
     '<option value="NU">Niue</option>'.
     '<option value="NF">Norfolk Island</option>'.
     '<option value="MP">Northern Mariana Islands</option>'.
     '<option value="NO">Norway</option>'.
     '<option value="OM">Oman</option>'.
     '<option value="PK">Pakistan</option>'.
     '<option value="PW">Palau</option>'.
     '<option value="PS">Palestine</option>'.
     '<option value="PA">Panama</option>'.
     '<option value="PG">Papua New Guinea</option>'.
     '<option value="PY">Paraguay</option>'.
     '<option value="PE">Peru</option>'.
     '<option value="PH">Philippines</option>'.
     '<option value="PN">Pitcairn</option>'.
     '<option value="PL">Poland</option>'.
     '<option value="PT">Portugal</option>'.
     '<option value="PR">Puerto Rico</option>'.
     '<option value="QA">Qatar</option>'.
     '<option value="RE">Reunion</option>'.
     '<option value="RO">Romania</option>'.
     '<option value="RU">Russian Federation</option>'.
     '<option value="RW">Rwanda</option>'.
     '<option value="BL">Saint Barthélemy</option>'.
     '<option value="SH">Saint Helena</option>'.
     '<option value="KN">Saint Kitts and Nevis</option>'.
     '<option value="LC">Saint Lucia</option>'.
     '<option value="MF">Saint Martin (French part)</option>'.
     '<option value="PM">Saint Pierre and Miquelon</option>'.
     '<option value="VC">Saint Vincent and the Grenadines</option>'.
     '<option value="WS">Samoa</option>'.
     '<option value="SM">San Marino</option>'.
     '<option value="ST">Sao Tome and Principe</option>'.
     '<option value="SA">Saudi Arabia</option>'.
     '<option value="SN">Senegal</option>'.
     '<option value="RS">Serbia</option>'.
     '<option value="SC">Seychelles</option>'.
     '<option value="SL">Sierra Leone</option>'.
     '<option value="SG">Singapore</option>'.
     '<option value="SK">Slovakia</option>'.
     '<option value="SI">Slovenia</option>'.
     '<option value="SB">Solomon Islands</option>'.
     '<option value="SO">Somalia</option>'.
     '<option value="ZA">South Africa</option>'.
     '<option value="GS">South Georgia and South Sandwich Islands</option>'.
     '<option value="KR">South Korea</option>'.
     '<option value="ES">Spain</option>'.
     '<option value="LK">Sri Lanka</option>'.
     '<option value="SD">Sudan</option>'.
     '<option value="SR">Suriname</option>'.
     '<option value="SJ">Svalbard and Jan Mayen Islands</option>'.
     '<option value="SZ">Swaziland</option>'.
     '<option value="SE">Sweden</option>'.
     '<option value="CH">Switzerland</option>'.
     '<option value="SY">Syria</option>'.
     '<option value="TW">Taiwan</option>'.
     '<option value="TJ">Tajikistan</option>'.
     '<option value="TZ">Tanzania</option>'.
     '<option value="TH">Thailand</option>'.
     '<option value="TL">Timor-Leste</option>'.
     '<option value="TG">Togo</option>'.
     '<option value="TK">Tokelau</option>'.
     '<option value="TO">Tonga</option>'.
     '<option value="TT">Trinidad and Tobago</option>'.
     '<option value="TN">Tunisia</option>'.
     '<option value="TR">Turkey</option>'.
     '<option value="TM">Turkmenistan</option>'.
     '<option value="TC">Turks and Caicos Islands</option>'.
     '<option value="TV">Tuvalu</option>'.
     '<option value="UG">Uganda</option>'.
     '<option value="UA">Ukraine</option>'.
     '<option value="AE">United Arab Emirates</option>'.
     '<option value="GB">United Kingdom</option>'.
     '<option value="UM">United States Minor Outlying Islands</option>'.
     '<option value="UY">Uruguay</option>'.
     '<option value="UZ">Uzbekistan</option>'.
     '<option value="VU">Vanuatu</option>'.
     '<option value="VA">Vatican City</option>'.
     '<option value="VE">Venezuela</option>'.
     '<option value="VN">Vietnam</option>'.
     '<option value="VG">Virgin Islands ( British )</option>'.
     '<option value="VI">Virgin Islands ( U.S. )</option>'.
     '<option value="WF">Wallis and Futuna Islands</option>'.
     '<option value="EH">Western Sahara</option>'.
     '<option value="YE">Yemen</option>'.
     '<option value="ZM">Zambia</option>'.
     '<option value="ZW">Zimbabwe</option>
</select>
<p>State/Province:</p>
<p id="stateSelect"><select name="state" id="state" disabled="disabled"><option value="Other">Select state...</option></select></p>
<table>
<tbody>
<tr>
<td><strong>Country value:</strong></td>
<td><p id="countryValue"></p></td>
</tr>
<tr>
<td><strong>State value:</strong></td>
<td><p id="stateValue"></p></td>
</tr>
</tbody></table>

After the user makes a country selection, the Javascript onChange event is fired and executes the printStateMenu Javascript function and passes the value of the country that was chosen as an argument. The table just displays the current values of the country and state select inputs. Please note that the script source path on line 1 will have to edited depending on where you store the JavaScript function. Here is the Javascript code for the printStateMenu function:

function printStateMenu(country) {     
	var stateSelect = '';
	if (country == 'US') {
		stateSelect = '<select name="state" id="state" onchange="document.getElementById(\'stateValue\').innerHTML = this.value;">'.
		'<option value="AK">AK-Alaska</option>'.
		'<option value="AL">AL-Alabama</option>'.
		'<option value="AR">AR-Arkansas</option>'.
		'<option value="AZ">AZ-Arizona</option>'.
		'<option value="CA">CA-California</option>'.
		'<option value="CO">CO-Colorado</option>'.
		'<option value="CT">CT-Connecticut</option>'.
		'<option value="DC">DC-District of Columbia</option>'.
		'<option value="DE">DE-Delaware</option>'.
		'<option value="FL">FL-Florida</option>'.
		'<option value="GA">GA-Georgia</option>'.
		'<option value="HI">HI-Hawaii</option>'.
		'<option value="IA">IA-Iowa</option>'.
		'<option value="ID">ID-Idaho</option>'.
		'<option value="IL">IL-Illinois</option>'.
		'<option value="IN">IN-Indiana</option>'.
		'<option value="KS">KS-Kansas</option>'.
		'<option value="KY">KY-Kentucky</option>'.
		'<option value="LA">LA-Louisiana</option>'.
		'<option value="MA">MA-Massachusetts</option>'.
		'<option value="MD">MD-Maryland</option>'.
		'<option value="ME">ME-Maine</option>'.
		'<option value="MI">MI-Michigan</option>'.
		'<option value="MN">MN-Minnesota</option>'.
		'<option value="MO">MO-Missouri</option>'.
		'<option value="MS">MS-Mississippi</option>'.
		'<option value="MT">MT-Montana</option>'.
		'<option value="NC">NC-North Carolina</option>'.
		'<option value="ND">ND-North Dakota</option>'.
		'<option value="NE">NE-Nebraska</option>'.
		'<option value="NH">NH-New Hampshire</option>'.
		'<option value="NJ">NJ-New Jersey</option>'.
		'<option value="NM">NM-New Mexico</option>'.
		'<option value="NV">NV-Nevada</option>'.
		'<option value="NY">NY-New York</option>'.
		'<option value="OH">OH-Ohio</option>'.
		'<option value="OK">OK-Oklahoma</option>'.
		'<option value="OR">OR-Oregon</option>'.
		'<option value="PA">PA-Pennsylvania</option>'.
		'<option value="RI">RI-Rhode Island</option>'.
		'<option value="SC">SC-South Carolina</option>'.
		'<option value="SD">SD-South Dakota</option>'.
		'<option value="TN">TN-Tennessee</option>'.
		'<option value="TX">TX-Texas</option>'.
		'<option value="UT">UT-Utah</option>'.
		'<option value="VA">VA-Virginia</option>'.
		'<option value="VT">VT-Vermont</option>'.
		'<option value="WA">WA-Washington</option>'.
		'<option value="WI">WI-Wisconsin</option>'.
		'<option value="WV">WV-West Virginia</option>'.
		'<option value="WY">WY-Wyoming</option>'.
		'</select>';
	}
	else if (country == 'CA') {
		stateSelect = '<select name="state" id="state" onchange="document.getElementById(\'stateValue\').innerHTML = this.value;">'.
		'<option value="AB">AB-Alberta</option>'.
		'<option value="BC">BC-British Columbia</option>'.
		'<option value="MB">MB-Manitoba</option>'.
		'<option value="NB">NB-New Brunswick</option>'.
		'<option value="NL">NL-Newfoundland and Labrador</option>'.
		'<option value="NT">NT-Northwest Territories</option>'.
		'<option value="NS">NS-Nova Scotia</option>'.
		'<option value="NU">NU-Nunavut</option>'.
		'<option value="ON">ON-Ontario</option>'.
		'<option value="PE">PE-Prince Edward Island</option>'.
		'<option value="QC">QC-Quebec</option>'.
		'<option value="SK">SK-Saskatchewan</option>'.
		'<option value="YT">YT-Yukon</option>'.
		'</select>';
	}
	else {
		stateSelect = '<select name="state" id="state" disabled>'.
		'<option value="Other">Select state...</option>'.
		'</select>';
	}
	document.getElementById('stateSelect').innerHTML = stateSelect;
	document.getElementById('countryValue').innerHTML = document.getElementById('country').value;
	document.getElementById('stateValue').innerHTML = document.getElementById('state').value;
}

This Javascript function is pretty simple. It looks at the country value that was selected and creates the corresponding state/province menu. Then it replaces the HTML code in the stateSelect DOM element with the new select menu. The state/province select inputs for US and Canada also have an onChange event that displays the chosen value of the state input into the table. The select menu for any other country will just return a disabled select menu and default to the Other value for the state input.

Published by

Allen Liu

Building websites is a passion of mine. During the course of my work, I have created some snippets of code that I hope will prove useful to other developers.

12 thoughts on “Dynamic Country State Javascript Menu Example”

  1. Thanks for this post, but please i have copied the entire code, yet it’s not working, i also remove the new line as you commented, but still not working.

    Help, Thanks

  2. I have a need for a show/hide that is not on your list…

    I need a show/hide, but I want it to be different in that I want it to check the text until it gets to a certain number of characters. It would then wrap a DIV from that position to the end of the text which could then be hidden with the hide() method. Then have a link to show the hidden text and then hide the text again.

    1. Hi Kathy,

      This is an interesting request. Can you possibly include more details on this? For example, is the # of characters fixed? how does it where to place the end DIV tag? The more specifications you can include the better.

      Allen

  3. Hi,

    Thanks for this code. I have question. If we want to have the same response like this script but the trigger is not a combolist but the text box, how to make it works?

    Please help me.

    Thanks alot.

  4. Hi Knix, im learning a lot from your site. Nice articles and easy to understand. im very new with this javascrpt and html, but i found your tutorial really helpful

    Ok, im trying to understand your post and i want to make something like this, the user has 3 options, first select country then state and the last one is town.

    I did try changing the script but it wont work for me..

    Can u do a little check for me..?

    here the example http://www.sempoi.biz/html/index.html

    p/s: sorry for my poor english

        1. Actually, you do get a JavaScript error. I highly recommend that you use FireFox with the FireBug plugin. It is extremely helpful for debugging JavaScript. Anyway, the error is related to an unterminated JavaScript code that you have:

          1. if (country == 'INDIA') {
          2.   stateSelect = '<select name="state" id="state">
          3.   <option value="Andhra Pradesh">Andhra Pradesh</option>
          4.   <option value="Arunachal Pradesh">Arunachal Pradesh</option>
          5.   <option value="Assam">Assam</option>
          6.   <option value="Bihar">Bihar</option>
          7.   <option value="Chhattisgarh">Chhattisgarh</option>
          8.   <option value="Goa">Goa</option>
          9.   <option value="Gujarat">Gujarat</option>
          10.   <option value="Haryana">Haryana</option>
          11.   <option value="Himachal Pradesh">Himachal Pradesh</option>
          12.   <option value="Jammu and Kashmir">Jammu and Kashmir</option>
          13.   <option value="Jharkhand">Jharkhand</option>
          14.   <option value="Karnataka">Karnataka</option>
          15.   <option value="Kerala">Kerala</option>
          16.   <option value="Madhya Pradesh">Madhya Pradesh</option>
          17.   <option value="Maharashtra">Maharashtra</option>
          18.   <option value="Manipur">Manipur</option>
          19.   <option value="Meghalaya">Meghalaya</option>
          20.   <option value="Mizoram">Mizoram</option>
          21.   <option value="Nagaland">Nagaland</option>
          22.   <option value="Orissa">Orissa</option>
          23.   <option value="Punjab">Punjab</option>
          24.   <option value="Rajasthan">Rajasthan</option>
          25.   <option value="Sikkim">Sikkim</option>
          26.   <option value="Tamil Nadu">Tamil Nadu</option>
          27.   <option value="Tripura">Tripura</option>
          28.   <option value="Uttar Pradesh">Uttar Pradesh</option>
          29.   <option value="Uttarakhand">Uttarakhand</option>
          30.   <option value="West Bengal">West Bengal</option>
          31.   <option value="Delhi">Delhi</option>
          32.   </select>';
          33.   }

          I think the newlines are killing this for you. Try something like this without the newlines between the single quotes:

          1.  if (country == 'INDIA') {
          2.    stateSelect = '<select name="state" id="state"><option value="Andhra Pradesh">Andhra Pradesh</option><option value="Arunachal Pradesh">Arunachal Pradesh</option><option value="Assam">Assam</option><option value="Bihar">Bihar</option><option value="Chhattisgarh">Chhattisgarh</option><option value="Goa">Goa</option></select>';
          3. }

Leave a Reply

Your email address will not be published. Required fields are marked *