Skip to content

Instantly share code, notes, and snippets.

@mpro9x
Created April 19, 2022 10:43
Show Gist options
  • Save mpro9x/e42fbe6e7d93982edba16908818f4694 to your computer and use it in GitHub Desktop.
Save mpro9x/e42fbe6e7d93982edba16908818f4694 to your computer and use it in GitHub Desktop.
Country Code JS version 2 make changes default country+code
<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