最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - On change event for country code in intlTelInput - Stack Overflow

matteradmin6PV0评论

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);
            
 });

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);
            
 });
Share Improve this question asked Jul 22, 2020 at 7:05 MithunMithun 2651 gold badge10 silver badges19 bronze badges 1
  • Please define "it's not working". – Teemu Commented Jul 22, 2020 at 7:12
Add a ment  | 

3 Answers 3

Reset to default 2

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('')
});

For those who want to change selection by country ID

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

try this, worked for me:

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

comment list (0)

  1. No comments so far