1 |
www |
1 |
// get the country data from the plugin
|
|
|
2 |
var countryData = window.intlTelInputGlobals.getCountryData(),
|
|
|
3 |
input = document.querySelector("#phone"),
|
|
|
4 |
addressDropdown = document.querySelector("#address-country");
|
|
|
5 |
|
|
|
6 |
// init plugin
|
|
|
7 |
var iti = window.intlTelInput(input, {
|
|
|
8 |
utilsScript: "../../build/js/utils.js?<%= time %>" // just for formatting/placeholders etc
|
|
|
9 |
});
|
|
|
10 |
|
|
|
11 |
// populate the country dropdown
|
|
|
12 |
for (var i = 0; i < countryData.length; i++) {
|
|
|
13 |
var country = countryData[i];
|
|
|
14 |
var optionNode = document.createElement("option");
|
|
|
15 |
optionNode.value = country.iso2;
|
|
|
16 |
var textNode = document.createTextNode(country.name);
|
|
|
17 |
optionNode.appendChild(textNode);
|
|
|
18 |
addressDropdown.appendChild(optionNode);
|
|
|
19 |
}
|
|
|
20 |
// set it's initial value
|
|
|
21 |
addressDropdown.value = iti.getSelectedCountryData().iso2;
|
|
|
22 |
|
|
|
23 |
// listen to the telephone input for changes
|
|
|
24 |
input.addEventListener('countrychange', function(e) {
|
|
|
25 |
addressDropdown.value = iti.getSelectedCountryData().iso2;
|
|
|
26 |
});
|
|
|
27 |
|
|
|
28 |
// listen to the address dropdown for changes
|
|
|
29 |
addressDropdown.addEventListener('change', function() {
|
|
|
30 |
iti.setCountry(this.value);
|
|
|
31 |
});
|