On change event for country code in intlTelInput

6.3k Views Asked by At

I am trying to set onchange event in intlTelInput (jQuery). But it's not working . If I change country code then it will listen as change . Here is my code. It's working when I change mobile number. But I want it when I change country code. Thanks in advance.

<input type="text" name="user_phone_number" id="user_phone_number"> 

$('input[name=user_phone_number]').change(function() {
     var countryCode = $("#user_phone_number").intlTelInput("getSelectedCountryData").dialCode;
     alert(countryCode);
            
 });
3

There are 3 best solutions below

0
On BEST ANSWER

check the documentation, you can use

input.addEventListener("countrychange", function() {
  // do something with iti.getSelectedCountryData()
});

and try this solution also

var input = $("#user_phone_number");
input.intlTelInput();

input.on("countrychange", function() {
  input.val('')
});
0
On

For those who want to change selection by country ID

$('#user_phone_number').intlTelInput("setCountry", "us");
1
On

try this, worked for me:

$("#ElementId").on("countrychange", function () {
                var SelectedCountry = $("#ElementId").intlTelInput("getSelectedCountryData");
                console.log(SelectedCountry );
           });