Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: $(…).intlTelInput is not a function #631

Closed
ryanandrews321 opened this issue Sep 7, 2017 · 22 comments
Closed

TypeError: $(…).intlTelInput is not a function #631

ryanandrews321 opened this issue Sep 7, 2017 · 22 comments

Comments

@ryanandrews321
Copy link

ryanandrews321 commented Sep 7, 2017

I'm trying to use intlTelInput for a HTML registration page but I am getting the following error:
Uncaught TypeError: $(...).intlTelInput is not a function

Here is the registration view code:


<html>
<head>
<!-- Page for customer registration and submit details to database to associate SSO with company etc -->
    <link rel="stylesheet" type="text/css" href="views/register.css">
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <link rel="stylesheet" href="../bower_components/intl-tel-input/build/css/intlTelInput.css">
    <script src="../bower_components/intl-tel-input/build/js/intlTelInput.js"></script>
    <script>
        $("#phoneNumber").intlTelInput();

        // get the country data from the plugin
        var countryData = $.fn.intlTelInput.getCountryData(),
            telInput = $("#phoneNumber"),
            addressDropdown = $("#country");

        // init plugin
        telInput.intlTelInput({
            utilsScript: "../bower_components/intl-tel-input/build/js/utils.js" // just for formatting/placeholders etc
        });

        // populate the country dropdown
        $.each(countryData, function(i, country) {
            addressDropdown.append($("<option></option>").attr("value", country.iso2).text(country.name));
        });
        // set it's initial value
        var initialCountry = telInput.intlTelInput("getSelectedCountryData").iso2;
        addressDropdown.val(initialCountry);

        // listen to the telephone input for changes
        telInput.on("countrychange", function(e, countryData) {
            addressDropdown.val(countryData.iso2);
        });

        // listen to the address dropdown for changes
        addressDropdown.change(function() {
            telInput.intlTelInput("setCountry", $(this).val());
        });
    </script>
</head>

<form name="page1" id="page1" role="form">

<div id="createCasePage1" class="container-fluid">

<div class="col-md-6 col-xs-12">
        <div class="form-group">
	      <label for="phoneNumber">Phone Number:</label>
          <input type="tel" id="phoneNumber" name="phoneNumber" ng-model="createUserObj[0].phoneNumber" class="form-control"  required>
      </div>
    </div>

    <div class="col-md-6 col-xs-12">
      <div class="form-group">
        <label for="country">Country:</label>
        <!--<input type="text" id="country" name="country" ng-model="createUserObj[0].country" class="form-control" placeholder="Country" required maxlength="100" /> -->
              <select id="country" name="country" ng-model="createUserObj[0].country" class="form-control" required></select>
      </div>
    </div>


    <!-- Check valid  inputs and patterns then submit to controller -->
    <button type="submit" id="register" class="btn btn-large" class="registerBtn" ng-click="page1.$valid && createUserFunc()">Register</button>
    <br>
    <br>
</div>
</form>
</html>

I think it's an issue with jQuery and Angular but I'm not really sure.

@jackocnr
Copy link
Owner

jackocnr commented Sep 7, 2017

You're calling $("#phoneNumber") before the page has loaded, so that wont work. You need to wrap that code in jQuery's document ready callback. Or move that script code to the end of the body.

@jackocnr jackocnr closed this as completed Sep 7, 2017
@ryanandrews321
Copy link
Author

I have now moved it around to this:

<html>
<head>
    <link rel="stylesheet" type="text/css" href="views/register.css">
</head>
<body>
<form name="page1" id="page1" role="form">

<div id="createCasePage1" class="container-fluid">

	<div class="col-md-6 col-xs-12">
        <div class="form-group">
	      <label for="phoneNumber">Phone Number:</label>
          <input type="tel" id="phoneNumber" name="phoneNumber" ng-model="createUserObj[0].phoneNumber" class="form-control"  required>
      </div>
    </div>

    <div class="col-md-6 col-xs-12">
      <div class="form-group">
        <label for="country">Country:</label>
        <!--<input type="text" id="country" name="country" ng-model="createUserObj[0].country" class="form-control" placeholder="Country" required maxlength="100" /> -->
              <select id="country" name="country" ng-model="createUserObj[0].country" class="form-control" required></select>
      </div>
    </div>

    <!-- Check valid  inputs and patterns then submit to controller -->
    <button type="submit" id="register" class="btn btn-large" class="registerBtn" ng-click="page1.$valid && createUserFunc()">Register</button>
    <br>
    <br>
</div>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <link rel="stylesheet" href="../bower_components/intl-tel-input/build/css/intlTelInput.css">
    <script src="../bower_components/intl-tel-input/build/js/intlTelInput.js"></script>
    <script>
        $("#phoneNumber").intlTelInput();

        // get the country data from the plugin
        var countryData = $.fn.intlTelInput.getCountryData(),
            telInput = $("#phoneNumber"),
            addressDropdown = $("#country");

        // init plugin
        telInput.intlTelInput({
            utilsScript: "../bower_components/intl-tel-input/build/js/utils.js" // just for formatting/placeholders etc
        });

        // populate the country dropdown
        $.each(countryData, function(i, country) {
            addressDropdown.append($("<option></option>").attr("value", country.iso2).text(country.name));
        });
        // set it's initial value
        var initialCountry = telInput.intlTelInput("getSelectedCountryData").iso2;
        addressDropdown.val(initialCountry);

        // listen to the telephone input for changes
        telInput.on("countrychange", function(e, countryData) {
            addressDropdown.val(countryData.iso2);
        });

        // listen to the address dropdown for changes
        addressDropdown.change(function() {
            telInput.intlTelInput("setCountry", $(this).val());
        });
    </script>
</form>
</body>
</html>

Still throwing the same error?
Can you show a working way to do it?

@jackocnr
Copy link
Owner

jackocnr commented Sep 7, 2017

If you reproduce the issue in a codepen I will take a look.

@ryanandrews321
Copy link
Author

Can I send you a link to the github repository?

@jackocnr
Copy link
Owner

jackocnr commented Sep 7, 2017

No, I need to see it up and running to debug it.

@fakerLinXiao
Copy link

fakerLinXiao commented Dec 12, 2017

same here, i used @types/ tel input.

@newgenesys
Copy link

Facing the same issue

@simonsviper
Copy link

I'm also having the same issue.

@Sakvojage
Copy link

Sakvojage commented May 31, 2018

Same here. Yii2 with borales\extensions\phoneInput\PhoneInput

@TomasHurtz
Copy link

TomasHurtz commented Jul 5, 2018

I had this issue. Just wrap the entire input code within;

           jQuery(document).ready(function() {	

                            telInput.intlTelInput({
			            initialCountry: "auto",
                                       stuff....

                });  // end docready

@A-Sahrawat
Copy link

This is the conflict issue with existing prototype. Try with add below line of code, hope this will help:

<script>
       jq = jQuery.noConflict();
       <!-- Replace $ with jq and call intlTelInput() function ------->
        use :
       jq("#phoneNumber").intlTelInput();
       insteadof :
        $("#phoneNumber").intlTelInput();
</script>

@anupamadhuri
Copy link

Having same issue after jquery upgraded to 3.1.1

Urgent!

@jackocnr
Copy link
Owner

jackocnr commented Aug 7, 2018

If you can reproduce the issue in a codepen (stripping down the code to the simplest possible form that reproduces the error), then I'm happy to take a look.

@A-Sahrawat
Copy link

May you please share your code sample.

@anupamadhuri
Copy link

<script type="text/javascript" src="/javascript/intlTelInput.js"></script>

at the end of page

<script> $(document).ready(function () { $('#PhoneCountryCode').intlTelInput(); $('.flag-dropdown').click(function () { addDialCodeExt(); }) }); function addDialCodeExt() { if ($('#PhoneCountryCode').val().indexOf('+') < 0) { $('#PhoneCountryCode').val("+"); } } </script>

@A-Sahrawat
Copy link

please share all code sample with html too.

@anupamadhuri
Copy link

anupamadhuri commented Aug 7, 2018

<script src="/javascript/2015/jquery-3.3.1.js"></script> <script src="/javascript/2016/jquery-ui-1.12.1.min.js"></script> <script src="/javascript/angular.min.js"></script> <script type="text/javascript" src="/javascript/intlTelInput.js?version=43.1"></script>
<div class="row col-sm-12">
    <div class="form-group">
        <label for="mobileNumber" class="col-sm-3 control-label">
            Mobile Number
        </label>

        <div class="col-sm-8 phone-parent-container">
            <div class="phone-number-container" style="display: flex;">
                <input type="text" name="countryCode" value="+1" class="form-control phone-code"
                       id="PhoneCountryCode">
                <input type="text" name="mobileNumber" value="" class="form-control" id="mobileNumber">
            </div>
        </div>
    </div>
</div>

</div>
<script> $(document).ready(function () { $('#PhoneCountryCode').intlTelInput(); $('.flag-dropdown').click(function () { addDialCodeExt(); }) }); function addDialCodeExt() { if ($('#PhoneCountryCode').val().indexOf('+') < 0) { $('#PhoneCountryCode').val("+"); } } </script>

@anupamadhuri
Copy link

Fixed
Solution:
In intlTelInput.js replace
.load(function() { ... });
To
.on('load', function() { ... });

@brada1703
Copy link

Fixed
Solution:
In intlTelInput.js replace
.load(function() { ... });
To
.on('load', function() { ... });

Where do you see this? I can't find this particular code in the file that you mentioned "intlTelInput.js". Note that I am using v14.0.4

@ronjb04
Copy link

ronjb04 commented Nov 15, 2018

Fixed
Solution:
In intlTelInput.js replace
.load(function() { ... });
To
.on('load', function() { ... });

i can't find this too in v14.0.5

@ishafayshahid
Copy link

Actually i'm using two input in form-group but when i'm going to use help-block it's working if any of two option is filled or select but i want it two working with particular input.

.....

I don't wanna use intlTelInput.js

@bala900
Copy link

bala900 commented May 19, 2020

$(document).ready(function()
{       
      var input = document.querySelector("#phoneNumber");
      var iti = window.intlTelInput(input, {
          utilsScript:'https://cdnjs.cloudflare.com/ajax/libs/intl-tel-input/17.0.0/js/utils.js',
          initialCountry:"auto",
          separateDialCode:true,
          dropdownContainer:null,
          geoIpLookup: function (callback) {
              $.get({url:"https://ipinfo.io",cache:true}, function () {}, "jsonp").always(function (resp) {
              var countryCode = (resp && resp.country) ? resp.country : "";                  
              this.country+=resp.country;                  
              callback(countryCode);
              });
          }   
       });      
 
    var handleChange = function() {
      //HERE YOU CAN GET THE MOBILE NUMBER
      console.log(iti.getNumber());  
    }

    input.addEventListener('change', handleChange);
    input.addEventListener('keyup', handleChange);
       
  });

<input matInput type="tel" class="form-contrl" [(ngModel)]="user.phoneNumber" name="phone" id="phoneNumber" autocomplete="off"/>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests