Dynamic country and region dropdowns for your forms.
A feature you often need in forms is a connected country and region dropdown, where the region field gets automatically
updated when the user selects a country. It’s very easy to code this of course, but it’s a pain having to track down
all the raw country-region data.
This script does all the legwork for you. It lets you add this feature to your form without having to write any code or
spend mind-numbing hours on Wikipedia downloading and formatting the data. I did that. It wasn’t pretty.
Pick your version:
The reason the files are so large (60K or more) is that they contain all the country and region strings. If you know
you’re only going to need a small subset of all countries, you may want to generate a custom build containing only that
info. That will substantially reduce the file size. See the Custom Builds section at the bottom of this page for more info.
Check out the following page for an example of the various ways it can be configured and used.
http://country-regions.github.io/country-region-selector/
Me, you - everyone! If you spot anything wonky for a particular country - out of date, incorrect or missing regions,
a pull request is very welcome. As of 0.3.0 the source data is found in a separate repo so it can be used by anyone:
https://github.com/country-regions/country-region-data
It’s very easy.
crs.min.js
file in your webpage.<select>
fields in the appropriate locations in your form.crs-country
.data-region-id="ABC"
where ABC is any string. NowBut here’s a few more tips for further configuration.
If you want to add default “Please select” options to either the country or region fields, just go ahead and add it
directly in the markup. The script will append the country and region <option>
fields - not overwrite them.
If your form is used for people returning to it (e.g. “Edit Contact Info” or whatever), you’ll need the country and
region fields to be prefilled with the appropriate value on page load. To do that, just add a data-default-value=""
attribute to each element containing the country / region value last saved. Note: the region value will only ever be
populated if the country field is as well.
This lists all the available data-* attributes that you can add to the country/region fields to customize the appearance
and behaviour.
data-region-id
- required. This should contain the ID of the region field that it’s being mapped to.data-default-option
- optional. Default: “Select country”. This determines the default, blank option display value.data-show-default-option
- optional. True by default. This shows the “Select Country” default option (or whateverdata-default-value
- optional. The default selected value in the country dropdown (e.g. “Canada”)data-value="shortcode"
- optional. The default behaviour is for the value attributes of the country dropdown options2-char
, but was renamed for consistency with the new region option. For backward compatibility 2-char
data-whitelist
- optional. A comma-delimited lists of country shortcodes that you want to appear in the dropdown.data-blacklist
- optional. Like the data-whitelist, only a blacklist! This lets you display all countries exceptdata-preferred
- optional. Lets you target specific countries to get listed at the top of the country dropdown.data-preferred="CA,US,MX"
.data-preferred-delim
- optional. If you use the data-preferred
option, you may want a line separating them fromdata-blank-option
- before the user selects a country, there’s a single data-default-option
- optional. Default: “Select region”. This determines the default, blank option display valuedata-show-default-option
- optional. True by default. This shows the “Select Region” default option (or whateverdata-default-value
- optional. The default selected value in the region dropdown (e.g. “British Columbia”, or “BC” if using the data-value=”shortcode” option)data-value="shortcode"
- optional. By default, region dropdowns will display the full region name. This option letsIn case your page is being generated on the fly, you’ll need to manually re-initialize the script after the new DOM
content is inserted.
With AMD (requireJS), just include the lib as you usually would. If you inspect the return value, you’ll see it has a
single init
function. Just call that method whenever you need it (i.e. after new DOM content is inserted into your
page).
define(['/path/to/crs.min'], function(crs) {
// when you're ready...
crs.init();
});
If you’re just including the crs.min.js in a <script>
tag in your page, it’ll automatically expose a crs
property
on your global window
object. Then you can call window.crs.init()
whenever your new page content has been dynamically
inserted. That will initialize the newly inserted country-region fields.
As of 0.2.4, you can generate a custom version of the library that contains only those countries you need. This can
substantially reduce the overall file size, if that’s important to you.
To do this, follow the instructions in the following section to get your dev environment set up, then instead of the
last step, run: grunt customBuild --countries="Canada,United States"
Just add whatever countries you want to include. To find the exact country names, take a look at the data in https://github.com/country-regions/country-region-data/blob/master/data.json.
This will generate new files in the /dist
folder that you can use.
If the country name you’re targeting contains a comma, just escape with with a single backslash, like so:grunt customBuild --countries="Côte d'Ivoire\, Republic of, Congo\, Republic of the (Brazzaville)"
If you want to edit the source code, go right ahead (pull requests welcome, of course!). The unminified source
is found in the /source
folder. To re-generate the minified version, just run the Grunt task. In case you’re not
familiar with Grunt, here’s how you get that hooked up.
npm install
to download all necessary require modules.npm install -g grunt-cli
to install the Grunt command line tool to run properly.grunt generate
That will then re-generate the minified files in your ./dist folder.
1.0.0
- Jan 17, 2023. Removing invalid console.log.0.5.1
- Nov 22, 2018. Fix for regions not being sorted alphabetically. Thanks, @VincentHokie!0.5.0
- July 8, 2018. Option to list preferred countries at top of countries dropdown added; country data updated. 0.4.1
- Nov 7, 2017. Country data updated.0.4.0
- Jul 25, 2017. Bug fix for blacklist/whitelisted fields in page with multiple other country-region-selectors0.3.6
- Oct 31, 2016. Country data updated.0.3.5
- July 4, 2016. Bug fix #40. Country data updated.0.3.4
- July 4, 2016. Bug fix #37. Country data updated.0.3.3
- June 7, 2016. Updated country-region-data set. 0.3.2
- May 15, 2016. More country shortcodes added - thanks Ellen Hutchings! Bug fixes0.3.1
- Apr 30, 2016. Loads of new country shortcodes added - thanks Ellen Hutchings!0.3.0
- Apr 28, 2016. Turkey region fix; source data moved to separate repo: https://github.com/country-regions/country-region-data0.2.4
- Feb 11, 2016. South Africa data updated. Custom build option added to let you generate smaller JS files0.2.3
- Feb 11, 2016. Indonesia, Mexico data updated.0.2.2
- Oct 22, 2015. Update regions of Spain to match postal regions.0.2.1
- Oct 20, 2015. Bug fix with Norwegian JS data.0.2.0
- Oct 18, 2015. data-whitelist
and data-blacklist
options added to Country dropdowns. Support fordata-value="shortcode"
option for Regions dropdowns. 0.1.9
- Aug 27, 2015. Option added to let you omit default option; NZ regions updated.0.1.8
- June 19, 2015. Fix for Norway regions.0.1.7
- May 2, 2015. Updated UK counties.0.1.6
- Apr 10, 2015. Russian regions updated by @Konair0s0.1.5
- Apr 5, 2015. Bug fix.0.1.4
- Apr 4, 2015. Lib now wrapped in UMD (accessible with RequireJS, CommonJS, Node); custom init() function exposed0.1.3
- Feb 10, 2015. Updated Ukraine regions.0.1.2
- Nov 1, 2014. Fix for typo in UK counties list.0.1.1
- April 24, 2014. Updated country list for ISO 3166 countries and short codes0.1.0
- March 27, 2014. Initial versionMIT.