Add Dropdown Country & State List Dynamically into HTML form by Javascript
You can use a JavaScript to add all the country names and state names’ Dropdown list. Which is actually done by HTML <select> and <insert> tag. I’ve written a JavaScript which can be simply inserted into your HTML file. Later, call my pre-defined functions and the country and state list will be automatically added into your HTML
Live Demo!
How to use?
1. Download Source Code (Latest Version) from here
Unzip and get the .js file
2. Put this code between your <HEAD> and </HEAD> section:
<script type= "text/javascript" src = "countries3.js"></script>
3. Put this code between your <BODY> and </BODY> section:
Select Country:
<select onchange="print_state('state',this.selectedIndex);" id="country" name = "country"></select> <br />
City/District/State:
<select name ="state" id = "state"></select>
<script language="javascript"<print_country("country");</script>
Please note that, we are using two <select> tags, which are actual drop-down boxes. Here I’ve chosen “country” and “state” as their ID attribute. You can choose any othe id attribute or name attribute as you like. But whatever you choose, you make to pass the correct IDs when calling the JavaScript Functions.
If you only want Country list to appear on the page, Simply use following bit of code in Body section:
Select Country: <select id="country" name ="country"></select>
<script language="javascript">print_country("country")</script>
You’ve to pass the ID of <select> element in print_country function, which actually prints the names of the countries.
Disclaimer: I’ve collected the names of countries and states from internet, and of course, cannot authenticate their validity!
Happy Coding
![]()

Paola Rozo 11:09 pm on November 25, 2009 Permalink |
Hi, I am working in your example,It works perfectly in Firefox but in IE does not insert any item in the list, Do you know what is the problem?
bdhacker 6:22 pm on January 16, 2010 Permalink |
Thanks for notifying me. I’ll work on it to find a valid solution
aglan 5:41 pm on January 16, 2010 Permalink |
hello,
ya it doesn’t work with ie,i have tried to put option tag and call the function i get all of them in one line.
any solutions?
bdhacker 7:53 pm on January 16, 2010 Permalink |
Dear Friend, I’ve fixed the bug
Please grab version 2.0 which works on IE:
http://countries.sourceforge.net/countries2.zip
David Underhill 12:24 am on May 7, 2010 Permalink |
Here is an alternative: http://javascript.internet.com/forms/country-state-drop-down.html
Glen 10:55 pm on August 8, 2010 Permalink |
Great if you only want US, Canada, UK and a VERY FEW other country states/provinces! Otherwise absolutely USELESS
Julian Woods 5:18 pm on May 10, 2010 Permalink |
Hi, thanks for the script. I’ve found that the State drop down doesn’t reset when going from a large list i.e. UK to a small list, the remainder of the options still stay there. I have added in “option_str.length=0;” to the print_state function to reset the list.
Regards
MuktoSource 11:07 pm on August 8, 2010 Permalink |
Thank yeah very much for the fix
Chinjith Sasidharan 4:27 pm on July 28, 2010 Permalink |
It saved my time. Very good script.
Countries2.zip eliminates all the bugs.
You are a good player of Javascript
Row content is correct. It contains all the states of India. Its perfect. I’m from India.
MuktoSource 8:57 pm on July 28, 2010 Permalink |
Thanks for your comment. I really appreciate it!
Glen 10:40 pm on August 8, 2010 Permalink |
I have a problem with this script: If someone accidentally selects the WRONG country, and then selects the right one, under the list of states etc, the states from the wrongly selected country will show below those from the current country. Is there a way to flush the state drop-down when another country is selected to avoid this happening?
Regards
Glen
Glen 10:56 pm on August 8, 2010 Permalink |
Just seen Julian’s comments above and used the ‘fix’ – that works perfectly.
Glen 4:15 pm on August 13, 2010 Permalink |
Got another problem here now. I need to dynamically insert the previously selected Country and State [read from the Member Db] as selected option, whilst still allowing the member to select any other Country and/or State. I am using asp and the db fields are called with the following code:
for Country:
for State:
99% of the time a member won’t be making a country change – more likely a state/province/county change.
Anyone have any ideas? I have tried the ‘ststic’ route by inserting:
<option selected value="”>
into the select box itself, but although the current state shows, the Country select box shows the ‘Select Country’ option.
Glen 4:17 pm on August 13, 2010 Permalink |
Bits got stripped out of my message. If you can help, please eMail me and I will give the full code.
Regards
Glen
Shafiul Azam 1:23 am on November 13, 2010 Permalink |
Here goes the code Glen mailed me. I think involves some asp.net/c# tags
mark 4:33 am on August 20, 2010 Permalink |
Hi, Would it be possible for you to let us know where you got the data from ?
I would like to populate some SQL tables with this data but it might be easier using the original source.
Regards,
Mark
Greg 7:44 pm on September 2, 2010 Permalink |
Hi, first off thanks for the script it is perfect for location entries.
I am having the same issue as Glen in that when a user goes to their edit profile page I want the current country and state/province they have in the db to be selected.
Is there a way to use an if statement within this code so that when the option fields are being generated if the current option equals what is in the database, that option will be selected.
Anyone have any ideas? I am better with PHP than Javascript so a push in the right direction would be appreciated. Glen did you solve your issue (again I am using PHP though)?
Glen 12:10 am on September 3, 2010 Permalink |
Unfortunately I haven’t solved this one yet. I am sure it is possible both in classic asp as I am using, and PHP as you’re using. It may just be a syntax thing in the javascript that I am getting wrong, so it ignores or ‘falls over’ when it tries to read the Db data in. I can get the country to work though … but it isn’t the country people would want to change – more the state.
Glen
JavaScript Country State List « Jan Souza – my Projects 2:56 am on October 14, 2010 Permalink |
[...] Project information http://bdhacker.wordpress.com/2009/11/21/adding-dropdown-country-state-list-dynamically-into-your-ht… [...]
Jan Souza 2:59 am on October 14, 2010 Permalink |
Hi,
Very simple and cool your project.
Thank you, help me a lot.
I put default select in your code.
http://jansouza.com/2010/10/13/javascript-country-state-list/
Feel free to use on your main code.
Thank you.
/*
*/
Prabu 5:33 pm on November 18, 2010 Permalink |
Hi
I am using this script to my page but it generate function(iteration) as many times in countries as well as state select boxes.. I need solution for this ! plz
Ross 6:48 am on November 22, 2010 Permalink |
after selecting a country such as USA then selecting another such as Australia I noticed the region/state option would append in FF.
option_str.options.length = 0; // inserting this will fix the issue
// put it just above the following line in print_state()
for(x in state_arr){
Shafiul Azam 1:03 pm on November 24, 2010 Permalink |
I’ve added Julian’s fix (also thanks to Ross to rediscover it
) and fix by Luigi Balzano
Released a new version (3.1)
Thanks guys for supporting this project
amp 7:07 am on November 28, 2010 Permalink |
Czeck Republic should be Czech Republic
amp 8:05 am on November 28, 2010 Permalink |
For a list of districts in New Zealand try: http://www.statoids.com/ynz.html
Rok 1:01 am on December 31, 2010 Permalink |
Awesome Script.
I added a few features fo my own use that you might want to add back into the code
Support for country and state codes
– US State Codes
– Canada Province codes
– Country Codes
– Support for easy reordering of countries
http://www.savrus-support.com/countries.zip
Thank you.
Andy 11:48 am on January 15, 2011 Permalink |
Hi,
Does anyone know where exactly in print_state’s function to put in “option_str.length=0;” ?
I am having the same problem as Glen. But I have no idea where to slot that code in.
I am no javascript-er.
Would appreciate if someone could answer this? Thanks!!
Andy 11:53 am on January 15, 2011 Permalink |
YIKES..sorry! My bad.
Realized i hd been using the outdated version of this thing didnt know there are newer files in the folders @ sourceforge.
THANKS
Jonathan 3:32 am on February 21, 2011 Permalink |
Anyone know how to incorporate this into a wordpress site?
eg)
A directory that is like craigslist, so same topics but in different cities, countries. People would search or add content only in their specific city or country.
ie ) Two locations: Los Angeles and New York. Type of places: clubs, bars, restaurant. Person can choose to search based on city and type of place.
Shafiul Azam 8:39 pm on February 21, 2011 Permalink |
I’ll extend it for wp
engiguide 6:15 pm on March 3, 2011 Permalink |
amazing one… ll help for my website form
engiguide 6:47 pm on March 3, 2011 Permalink |
also try for cities for all countries .. its big task want help from me mail me to ajaypatel_aj@yahoo.com
Sena Ilic 6:21 pm on April 20, 2011 Permalink |
Terrific submit man. For those who are enthusiastic about Cura de slabire evening me.
DJ 3:53 pm on April 28, 2011 Permalink |
Very good script, but I need country/state data list on XML spreadsheet (column A : country and column B : related state) I’m appreciated if someone can help me put them on XML file.
mernslors 2:39 am on June 22, 2011 Permalink |
Hello all! I like this forum, i organize many inviting people on this forum.!!!
Great Community, respect all!
Rojan 12:42 pm on July 8, 2011 Permalink |
thank you…
ram 1:18 pm on July 28, 2011 Permalink |
hi i tried to use it with jquery.jqtransform.js it doesn’t work.
Thas 4:09 pm on October 24, 2011 Permalink |
very Good Script .thank you………
ganapathi 1:10 pm on December 12, 2011 Permalink |
just intimate print error on display instead this print_country(“country”);
our code is <script language="javascript"<print_country("country");
its wonder
rlearning 2:46 pm on December 20, 2011 Permalink |
I noticed that the list does not clear out completely if you end up selecting more than 1 country (user sometimes
could select the wrong country)
As a test I selected Vietnam first, then selected a city in the list. I then selected USA.
When you look in the list of cities it now shows both USA cities and part of the Vietnam cities in the list.
I tried this with both Firefox (ver 8) and Chrome (ver 16).
Besides that it is a good script and saved me a lot of coding.
ilesh 6:33 pm on December 24, 2011 Permalink |
thankx for this and it’s a good script
melvin 7:14 pm on December 30, 2011 Permalink |
So far i facing 1 problem, I cannot get the selected value from the list.
I use runat = “server” between the , so that i can use it at code behind :
protected void Button1_Click(object sender, EventArgs e)
{
this.Label1.Text = country.Value.ToString();
}
It always return ‘ ‘, or ’0′ if i change the ‘Value’ to ‘SelectedIndex’. someone please help me =(
Sonny 2:20 am on January 23, 2012 Permalink |
I modified Jan Souza’s code further by adding default State along with his default Country. Here’s the changes to the last two functions.
function print_country(country_id, selected, state_selected) {
var indexSelected = 0;
// given the id of the tag as function argument, it inserts tags
var option_str = document.getElementById(country_id);
var x, i = 0;
for (x in country_arr) {
option_str.options[i++] = new Option(country_arr[x], country_arr[x]);
if (selected && country_arr[x] == selected) {
//alert(selected);
indexSelected = i – 1;
}
}
if (selected) {
option_str.selectedIndex = indexSelected;
print_state(‘state’, indexSelected, state_selected);
}
}
function print_state(state_id, state_index, state_selected) {
var option_str = document.getElementById(state_id);
option_str.length = 0; // Fixed by Julian Woods
option_str.options[0] = new Option(‘Select State’, ”);
option_str.selectedIndex = 0;
var state_arr = s_a[state_index + 1].split(“|”);
for (var i = 0; i < state_arr.length; i++) {
if (state_arr[i].toLowerCase() == state_selected.toLowerCase())
option_str.options[option_str.length] = new Option(state_arr[i], state_arr[i], true, true);
else
option_str.options[option_str.length] = new Option(state_arr[i], state_arr[i]);
}
}
To use, add the following to the HTML:
print_country(“country”, “USA”, “Virginia”);
Enjoy!
Mitul 2:00 pm on January 28, 2012 Permalink |
Hi Shafiul Azam,
I really appriciate your work.
But if you can provide cities then please inform me as soon as possible, i need them.
Naresh 5:37 pm on February 21, 2012 Permalink |
Hi It is very nice code & Useful. It save time
but i face same problem that melvin have.
i can’t get the user selected value
Shafiul Azam 6:27 pm on February 21, 2012 Permalink |
Are you using my code directly/made any other modifications? It’s suppose to work as standalone javascript only!