Giter VIP home page Giter VIP logo

country-select-js's Introduction

Country Select JS

A jQuery plugin for selecting a country, based on the excellent International Telephone Input plugin. It adds a flag dropdown to any input, which lists all the countries in English and the predominant national language next to their flags.

alt tag

Table of Contents

Demo

Try it for yourself using the included demo.html.

Features

  • Automatically select the country as the user types
  • Navigate the country dropdown by typing a country's name, or using up/down keys
  • Selecting a country from the dropdown will update the country name in the input
  • Dropdown appears above or below the input depending on available space/scroll position
  • Lots of initialisation options for customisation, as well as public methods for interaction
  • Can optionally update a related field with the two-letter ISO country code on selection

Getting Started

  1. Download the latest version

  2. Link the stylesheet (note that this references the image flags.png)

<link rel="stylesheet" href="build/css/countrySelect.css">
  1. Add the plugin script and initialise it on your input element
<input type="text" id="country">

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="build/js/countrySelect.min.js"></script>
<script>
  $("#country").countrySelect();
</script>
  1. Optional: add an extra input field (with type hidden or text) named the same as your selector input appended with "_code". This will automatically be updated with the ISO 3166-1 alpha-2 code for the selected country.
<input type="text" id="country" />
<input type="hidden" id="country_code" />

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="build/js/countrySelect.min.js"></script>
<script>
  $("#country").countrySelect();
</script>

Options

Note: any options that take country codes should be lower case ISO 3166-1 alpha-2 codes

defaultCountry
Type: String Default: ""
Set the default country by it's country code. Otherwise it will just be the first country in the list.

onlyCountries
Type: Array Default: undefined
Display only the countries you specify. Takes an array of country codes.

preferredCountries
Type: Array Default: ["us", "gb"]
Specify the countries to appear at the top of the list.

responsiveDropdown
Type: Boolean Default: false
Set the dropdown's width to be the same as the input. This is automatically enabled for small screens.

Public Methods

destroy
Remove the plugin from the input, and unbind any event listeners.

$("#country").countrySelect("destroy");

getSelectedCountryData
Get the country data for the currently selected flag.

var countryData = $("#country").countrySelect("getSelectedCountryData");

Returns something like this:

{
  name: "Afghanistan (‫افغانستان‬‎)",
  iso2: "af",
}

selectCountry
Change the country selection (e.g. when the user is entering their address).

$("#country").countrySelect("selectCountry", "gb");

setCountry
Insert a country name, and update the selected flag accordingly.

$("#country").countrySelect("setCountry", "United States");

Static Methods

getCountryData
Get all of the plugin's country data.

var countryData = $.fn.countrySelect.getCountryData();

Returns an array of country objects:

[{
  name: "Afghanistan (‫افغانستان‬‎)",
  iso2: "af",
}, ...]

setCountryData
Set all of the plugin's country data.

$.fn.countrySelect.setCountryData(countryData);

Troubleshooting

Image path
Depending on your project setup, you may need to override the path to flags.png in your CSS.

.country-select-input .flag {background-image: url("path/to/flags.png");}

Full width input
If you want your input to be full-width, you need to set the container to be the same i.e.

.country-select-input {width: 100%;}

Input margin
For the sake of alignment, the default CSS forces the input's vertical margin to 0px. If you want vertical margin, you should add it to the container (with class country-select-input).

Displaying error messages
If your error handling code inserts an error message before the <input> it will break the layout. Instead you must insert it before the container (with class country-select-input).

Dropdown position
The dropdown should automatically appear above/below the input depending on the available space. For this to work properly, you must only initialise the plugin after the <input> has been added to the DOM.

Contributing

Contributions and improvements to the library are welcome! For instructions on contributing to a project on Github, see this guide: Fork A Repo.

Attributions

country-select-js's People

Contributors

mrmarkfrench avatar zwalden avatar benmurden avatar nampnq avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.