intl-tel-input icon indicating copy to clipboard operation
intl-tel-input copied to clipboard

setNumber/getNumber

Open Raketten1963 opened this issue 2 years ago • 6 comments

Steps to reproduce

  1. create phone input object
  2. call setNumber(valid number)
  3. call getNumber()

Expected behaviour

getNumber should return valid number provided in the setNumber call

Actual behaviour

getNumber return empty string if calling setTimeout(getNumber,2000) the right number will be returned, so empty string is only returned if calling getNumber immediatly after calling setNumber

Initialisation options

        autoPlaceholder: "aggressive",
        initialCountry: "auto",
        geoIpLookup: function(success) {
            // Get your api-key at https://ipdata.co/
            fetch("https://api.ipdata.co/?api-key=mykey")
              .then(function(response) {
                if (!response.ok) return success("");
                return response.json();
              })
              .then(function(ipdata) {
                success(ipdata.country_code);
              });
          },
        separateDialCode: true,
        preferredCountries: ["us", "dk", "se", "de", "tw"],
        utilsScript:
        "https://cdnjs.cloudflare.com/ajax/libs/intl-tel-input/17.0.8/js/utils.js",

Raketten1963 avatar Apr 06 '22 05:04 Raketten1963

If you can find the minimal set of code that reproduces the problem and put that in a CodePen, then I'd be happy to take a look. Here is an example pen with the plugin up and running - feel free to fork this if it helps.

jackocnr avatar May 30 '22 21:05 jackocnr

I'm having the same issue, still not fixed.

function setDefaultNumber() { let preloadedNumber = '308113853'; phoneInput.setNumber(preloadedNumber); console.log(phoneInput.getNumber()); }

With this, I got nothing in the console.

boldizsarf avatar Jun 21 '22 15:06 boldizsarf

@boldizsarf I was having issues with this, it may not help but my issue was actually because I was using an outdated jQuery with the version of int-tel-input. I'd make sure that you're using the latest version of both int-tel-input and jQuery if not doing so already

ca-soho avatar Jun 22 '22 07:06 ca-soho

I'm using the latest jQuery and the latest version of int-tel-input, and still not working.

boldizsarf avatar Jun 22 '22 09:06 boldizsarf

@jackocnr here's the pen that reproduces the bug https://codepen.io/fetis/pen/oNqNVQK?editors=1111

if you call it immediately -> empty string if you click button to get a value -> value is returned but not in international format

fetis avatar Jun 29 '22 13:06 fetis

It's seems the issue comes from the "window.inTelInputUtils" which is not yet loaded when the first call of getNumber() is called due to the fact the documentReady() function return false because the document.readState is "loading" and not yet complete. So, while the window.AddEventListener which listen the "load" event is not trigger, and which seems coming after the call of getNumber(), issue seems there. This listener is defined in the code of the "_initRequests" function.

autb avatar Sep 20 '22 09:09 autb

Had the same issue. In my case, it was because my intlTelInput was not initialized quick enough before I attempted to get the number. Initializing it early before I attempted to get the phone number worked.

thhVictor avatar Feb 28 '23 07:02 thhVictor

As others have pointed out, this is a race condition. getNumber only works when the utils script has been loaded, which is an async process, so you need to make sure that has finished before you call it. If you check the docs for utilsScript option, you will see info about how the initialisation returns a promise that you can use to know when it's finished loading.

jackocnr avatar Apr 07 '23 11:04 jackocnr