-
-
Save mpro9x/e42fbe6e7d93982edba16908818f4694 to your computer and use it in GitHub Desktop.
Country Code JS version 2 make changes default country+code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script src='https://intl-tel-input.com/node_modules/intl-tel-input/build/js/intlTelInput.js?1549804213570'></script> | |
<script> | |
// International telephone format | |
// $("#phone").intlTelInput(); | |
// get the country data from the plugin | |
var countryData = window.intlTelInputGlobals.getCountryData(), | |
input = document.querySelector("#phone"), | |
addressDropdown = document.querySelector("#country"); | |
// init plugin | |
var iti = window.intlTelInput(input, { | |
hiddenInput: "full_phone", | |
utilsScript: "https://intl-tel-input.com/node_modules/intl-tel-input/build/js/utils.js", // just for formatting/placeholders etc | |
initialCountry: "au" // for country, you can change it for default | |
}); | |
// 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); | |
}); | |
</script> | |
<script> | |
//Append Value To Phone Field | |
$("#phone").prop('value', '+61 '); //for country code, you can change it for default | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Source: https://stackoverflow.com/questions/57406132/intl-tel-input-change-dropdown-dialcode-from-another-dropdown