AutorÃa | Ultima modificación | Ver Log |
<!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?1603274336113"><link rel="stylesheet" href="../../build/css/demo.css?1603274336113"><link rel="stylesheet" href="../css/countrySync.css?1603274336113"><!-- 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 pluginvar countryData = window.intlTelInputGlobals.getCountryData(),input = document.querySelector("#phone"),addressDropdown = document.querySelector("#address-country");// init pluginvar iti = window.intlTelInput(input, {utilsScript: "../../build/js/utils.js?1603274336113" // just for formatting/placeholders etc});// populate the country dropdownfor (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 valueaddressDropdown.value = iti.getSelectedCountryData().iso2;// listen to the telephone input for changesinput.addEventListener('countrychange', function(e) {addressDropdown.value = iti.getSelectedCountryData().iso2;});// listen to the address dropdown for changesaddressDropdown.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?1603274336113"></script><script src="./js/countrySync.js?1603274336113"></script></body></html>