<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Example: Country sync</title> <link rel="stylesheet" href="../css/prism.css"> <link rel="stylesheet" href="../../build/css/intlTelInput.css?1541153396801"> <link rel="stylesheet" href="../../build/css/demo.css?1541153396801"> <link rel="stylesheet" href="../css/countrySync.css?1541153396801"> <!-- GOOGLE ANALYTICS --> <script> (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) })(window,document,'script','https://www.google-analytics.com/analytics.js','ga'); ga('create', 'UA-85394876-1', 'auto'); ga('send', 'pageview'); </script> <!-- /GOOGLE ANALYTICS --> </head> <body> <a href="/">Back</a> <h1>Example: Country sync</h1> <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> <h2>Markup</h2> <pre><code class="language-markup"><div class="form-item"> <label>Telephone number</label> <input id="phone" type="tel"> </div> <div class="form-item"> <label>Address</label> <input type="text" placeholder="House name/number"> <input type="text" placeholder="City"> <input type="text" placeholder="State"> <input type="text" placeholder="Zip code"> <select id="address-country"></select> </div></code></pre> <h2>Code</h2> <pre><code class="language-javascript">// get the country data from the plugin var countryData = window.intlTelInputGlobals.getCountryData(), input = document.querySelector("#phone"), addressDropdown = document.querySelector("#address-country"); // init plugin var iti = window.intlTelInput(input, { utilsScript: "../../build/js/utils.js?1541153396801" // just for formatting/placeholders etc }); // populate the country dropdown for (var i = 0; i < countryData.length; i++) { var country = countryData[i]; var optionNode = document.createElement("option"); optionNode.value = country.iso2; var textNode = document.createTextNode(country.name); optionNode.appendChild(textNode); addressDropdown.appendChild(optionNode); } // set it's initial value addressDropdown.value = iti.getSelectedCountryData().iso2; // listen to the telephone input for changes input.addEventListener('countrychange', function(e) { addressDropdown.value = iti.getSelectedCountryData().iso2; }); // listen to the address dropdown for changes addressDropdown.addEventListener('change', function() { iti.setCountry(this.value); }); </code></pre> <h2>Result</h2> <div id="result"> <div class="form-item"> <label>Telephone number</label> <input id="phone" type="tel"> </div> <div class="form-item"> <label>Address</label> <input type="text" placeholder="House name/number"> <input type="text" placeholder="City"> <input type="text" placeholder="State"> <input type="text" placeholder="Zip code"> <select id="address-country"></select> </div> </div> <script src="../js/prism.js"></script> <script src="../../build/js/intlTelInput.js?1541153396801"></script> <script src="./js/countrySync.js?1541153396801"></script> </body> </html>