wefra/lib/intl-tel-input-master/examples/gen/country-sync.html

101 lines
3.6 KiB
HTML

<!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">&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?1541153396801&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?1541153396801"></script>
<script src="./js/countrySync.js?1541153396801"></script>
</body>
</html>