UNPKG

intl-tel-input

Version:

A JavaScript plugin for entering and validating international telephone numbers

99 lines (84 loc) 3.52 kB
<!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?1638200991544"> <link rel="stylesheet" href="../../build/css/demo.css?1638200991544"> <link rel="stylesheet" href="../css/countrySync.css?1638200991544"> <!-- Global site tag (gtag.js) - Google Analytics --> <script async src="https://www.googletagmanager.com/gtag/js?id=G-N472J4QKC4"></script> <script> window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'G-N472J4QKC4'); </script> </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">&lt;div class=&quot;form-item&quot;&gt; &lt;label&gt;Telephone number&lt;/label&gt; &lt;input id=&quot;phone&quot; type=&quot;tel&quot;&gt; &lt;/div&gt; &lt;div class=&quot;form-item&quot;&gt; &lt;label&gt;Address&lt;/label&gt; &lt;input type=&quot;text&quot; placeholder=&quot;House name/number&quot;&gt; &lt;input type=&quot;text&quot; placeholder=&quot;City&quot;&gt; &lt;input type=&quot;text&quot; placeholder=&quot;State&quot;&gt; &lt;input type=&quot;text&quot; placeholder=&quot;Zip code&quot;&gt; &lt;select id=&quot;address-country&quot;&gt;&lt;/select&gt; &lt;/div&gt;</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(&quot;#phone&quot;), addressDropdown = document.querySelector(&quot;#address-country&quot;); // init plugin var iti = window.intlTelInput(input, { utilsScript: &quot;../../build/js/utils.js?1638200991544&quot; // just for formatting/placeholders etc }); // populate the country dropdown for (var i = 0; i &lt; countryData.length; i++) { var country = countryData[i]; var optionNode = document.createElement(&quot;option&quot;); optionNode.value = country.iso2; var textNode = document.createTextNode(country.name); optionNode.appendChild(textNode); addressDropdown.appendChild(optionNode); } // set it&#39;s initial value addressDropdown.value = iti.getSelectedCountryData().iso2; // listen to the telephone input for changes input.addEventListener(&#39;countrychange&#39;, function(e) { addressDropdown.value = iti.getSelectedCountryData().iso2; }); // listen to the address dropdown for changes addressDropdown.addEventListener(&#39;change&#39;, 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?1638200991544"></script> <script src="./js/countrySync.js?1638200991544"></script> </body> </html>