1 |
www |
1 |
<!DOCTYPE html>
|
|
|
2 |
<html>
|
|
|
3 |
<head>
|
|
|
4 |
<meta charset="utf-8">
|
|
|
5 |
<title>Example: Country sync</title>
|
|
|
6 |
<link rel="stylesheet" href="../css/prism.css">
|
|
|
7 |
<link rel="stylesheet" href="../../build/css/intlTelInput.css?1603274336113">
|
|
|
8 |
<link rel="stylesheet" href="../../build/css/demo.css?1603274336113">
|
|
|
9 |
|
|
|
10 |
<link rel="stylesheet" href="../css/countrySync.css?1603274336113">
|
|
|
11 |
|
|
|
12 |
|
|
|
13 |
<!-- GOOGLE ANALYTICS -->
|
|
|
14 |
<script>
|
|
|
15 |
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
|
|
|
16 |
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
|
|
|
17 |
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
|
|
|
18 |
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
|
|
|
19 |
ga('create', 'UA-85394876-1', 'auto');
|
|
|
20 |
ga('send', 'pageview');
|
|
|
21 |
</script>
|
|
|
22 |
<!-- /GOOGLE ANALYTICS -->
|
|
|
23 |
</head>
|
|
|
24 |
|
|
|
25 |
<body>
|
|
|
26 |
<a href="/">Back</a>
|
|
|
27 |
<h1>Example: Country sync</h1>
|
|
|
28 |
<p>Use static getCountryData method to create a separate country dropdown for an address form, and then listen for change events to keep the two dropdowns in sync.</p>
|
|
|
29 |
|
|
|
30 |
<h2>Markup</h2>
|
|
|
31 |
<pre><code class="language-markup"><div class="form-item">
|
|
|
32 |
<label>Telephone number</label>
|
|
|
33 |
<input id="phone" type="tel">
|
|
|
34 |
</div>
|
|
|
35 |
|
|
|
36 |
<div class="form-item">
|
|
|
37 |
<label>Address</label>
|
|
|
38 |
<input type="text" placeholder="House name/number">
|
|
|
39 |
<input type="text" placeholder="City">
|
|
|
40 |
<input type="text" placeholder="State">
|
|
|
41 |
<input type="text" placeholder="Zip code">
|
|
|
42 |
<select id="address-country"></select>
|
|
|
43 |
</div></code></pre>
|
|
|
44 |
|
|
|
45 |
<h2>Code</h2>
|
|
|
46 |
<pre><code class="language-javascript">// get the country data from the plugin
|
|
|
47 |
var countryData = window.intlTelInputGlobals.getCountryData(),
|
|
|
48 |
input = document.querySelector("#phone"),
|
|
|
49 |
addressDropdown = document.querySelector("#address-country");
|
|
|
50 |
|
|
|
51 |
// init plugin
|
|
|
52 |
var iti = window.intlTelInput(input, {
|
|
|
53 |
utilsScript: "../../build/js/utils.js?1603274336113" // just for formatting/placeholders etc
|
|
|
54 |
});
|
|
|
55 |
|
|
|
56 |
// populate the country dropdown
|
|
|
57 |
for (var i = 0; i < countryData.length; i++) {
|
|
|
58 |
var country = countryData[i];
|
|
|
59 |
var optionNode = document.createElement("option");
|
|
|
60 |
optionNode.value = country.iso2;
|
|
|
61 |
var textNode = document.createTextNode(country.name);
|
|
|
62 |
optionNode.appendChild(textNode);
|
|
|
63 |
addressDropdown.appendChild(optionNode);
|
|
|
64 |
}
|
|
|
65 |
// set it's initial value
|
|
|
66 |
addressDropdown.value = iti.getSelectedCountryData().iso2;
|
|
|
67 |
|
|
|
68 |
// listen to the telephone input for changes
|
|
|
69 |
input.addEventListener('countrychange', function(e) {
|
|
|
70 |
addressDropdown.value = iti.getSelectedCountryData().iso2;
|
|
|
71 |
});
|
|
|
72 |
|
|
|
73 |
// listen to the address dropdown for changes
|
|
|
74 |
addressDropdown.addEventListener('change', function() {
|
|
|
75 |
iti.setCountry(this.value);
|
|
|
76 |
});
|
|
|
77 |
</code></pre>
|
|
|
78 |
|
|
|
79 |
<h2>Result</h2>
|
|
|
80 |
<div id="result">
|
|
|
81 |
<div class="form-item">
|
|
|
82 |
<label>Telephone number</label>
|
|
|
83 |
<input id="phone" type="tel">
|
|
|
84 |
</div>
|
|
|
85 |
|
|
|
86 |
<div class="form-item">
|
|
|
87 |
<label>Address</label>
|
|
|
88 |
<input type="text" placeholder="House name/number">
|
|
|
89 |
<input type="text" placeholder="City">
|
|
|
90 |
<input type="text" placeholder="State">
|
|
|
91 |
<input type="text" placeholder="Zip code">
|
|
|
92 |
<select id="address-country"></select>
|
|
|
93 |
</div>
|
|
|
94 |
</div>
|
|
|
95 |
|
|
|
96 |
<script src="../js/prism.js"></script>
|
|
|
97 |
<script src="../../build/js/intlTelInput.js?1603274336113"></script>
|
|
|
98 |
<script src="./js/countrySync.js?1603274336113"></script>
|
|
|
99 |
</body>
|
|
|
100 |
</html>
|