javascript - add country code in the dropdown of intl-country-code dropdown in jquery - Stack Overflow

I am using an intl-tel-input plugin to get the country code dropdown along with the country flag. I hav

I am using an intl-tel-input plugin to get the country code dropdown along with the country flag. I have another input field that has a country name as a dropdown. what I want is when the user selects any country from the country dropdown, then country code automatically gets selected in the country code dropdown. I used several methods but nothing works for me, I didn't find suitable documentation of this plugin too. this is my code.

<html>
    <head>
        <link rel="stylesheet" href=".0.8/css/intlTelInput.css" />
    </head>
    <body>
        <div class="container-fluid">
            <div class="row">
                <h1>Select Country</h1>
            </div>
            <div class="row">
                <div class="col-md-12 form-group">
                    <div class="col-md-6 form-group hire-input-country_box position-relative">
                        <select name="country" class="form-control" id="country">
                            <option data-countryCode="DZ" value="213">Algeria</option>
                            <option data-countryCode="AD" value="376">Andorra</option>
                            <option data-countryCode="AO" value="244">Angola</option>
                            <option data-countryCode="AI" value="1264">Anguilla</option>
                            <option data-countryCode="AG" value="1268">Antigua &amp; Barbuda
                            </option>
                        </select>
                    </div>
                </div>
                <div class="col-md-12 form-group">
                    <input type="tel" id="txtPhone" class="txtbox" />
                </div>
            </div>
        </div>
        <!-- Use as a jQuery plugin -->
        <script type="text/javascript" src=".4.1.min.js"></script>
        <script type="text/javascript" src=".0.8/js/intlTelInput-jquery.min.js"></script>
        <script type="text/javascript">
            $(function () {

                $("#country").change(function()
                {
                    let value="+"+$(this).val();
                    $('#txtPhone').val(value);

                })
                var code = "+977"; // Assigning value from model.
                $('#txtPhone').val(code);
                $('#txtPhone').intlTelInput({
                });
            });
        </script>
    </body>
</html>

I am using an intl-tel-input plugin to get the country code dropdown along with the country flag. I have another input field that has a country name as a dropdown. what I want is when the user selects any country from the country dropdown, then country code automatically gets selected in the country code dropdown. I used several methods but nothing works for me, I didn't find suitable documentation of this plugin too. this is my code.

<html>
    <head>
        <link rel="stylesheet" href="https://cdnjs.cloudflare./ajax/libs/intl-tel-input/16.0.8/css/intlTelInput.css" />
    </head>
    <body>
        <div class="container-fluid">
            <div class="row">
                <h1>Select Country</h1>
            </div>
            <div class="row">
                <div class="col-md-12 form-group">
                    <div class="col-md-6 form-group hire-input-country_box position-relative">
                        <select name="country" class="form-control" id="country">
                            <option data-countryCode="DZ" value="213">Algeria</option>
                            <option data-countryCode="AD" value="376">Andorra</option>
                            <option data-countryCode="AO" value="244">Angola</option>
                            <option data-countryCode="AI" value="1264">Anguilla</option>
                            <option data-countryCode="AG" value="1268">Antigua &amp; Barbuda
                            </option>
                        </select>
                    </div>
                </div>
                <div class="col-md-12 form-group">
                    <input type="tel" id="txtPhone" class="txtbox" />
                </div>
            </div>
        </div>
        <!-- Use as a jQuery plugin -->
        <script type="text/javascript" src="https://code.jquery./jquery-3.4.1.min.js"></script>
        <script type="text/javascript" src="https://cdnjs.cloudflare./ajax/libs/intl-tel-input/16.0.8/js/intlTelInput-jquery.min.js"></script>
        <script type="text/javascript">
            $(function () {

                $("#country").change(function()
                {
                    let value="+"+$(this).val();
                    $('#txtPhone').val(value);

                })
                var code = "+977"; // Assigning value from model.
                $('#txtPhone').val(code);
                $('#txtPhone').intlTelInput({
                });
            });
        </script>
    </body>
</html>

Share Improve this question asked Mar 28, 2022 at 10:20 Pranay kumarPranay kumar 2,1974 gold badges31 silver badges59 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 3

To do what you require, call the setCountry option of intlTelInput and provide the country-code data attribute value from the selected option:

$(function() {
  $("#country").change(function() {
    let countryCode = $(this).find('option:selected').data('country-code');
    let value = "+" + $(this).val();
    $('#txtPhone').val(value).intlTelInput("setCountry", countryCode);
  });
  
  var code = "+977";
  $('#txtPhone').val(code).intlTelInput();
});
<div class="container-fluid">
  <div class="row">
    <h1>Select Country</h1>
  </div>
  <div class="row">
    <div class="col-md-12 form-group">
      <div class="col-md-6 form-group hire-input-country_box position-relative">
        <select name="country" class="form-control" id="country">
          <option data-country-code="DZ" value="213">Algeria</option>
          <option data-country-code="AD" value="376">Andorra</option>
          <option data-country-code="AO" value="244">Angola</option>
          <option data-country-code="AI" value="1264">Anguilla</option>
          <option data-country-code="AG" value="1268">Antigua &amp; Barbuda
          </option>
        </select>
      </div>
    </div>
    <div class="col-md-12 form-group">
      <input type="tel" id="txtPhone" class="txtbox" />
    </div>
  </div>
</div>

<script type="text/javascript" src="https://code.jquery./jquery-3.4.1.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare./ajax/libs/intl-tel-input/16.0.8/js/intlTelInput-jquery.min.js"></script>

<link rel="stylesheet" href="https://cdnjs.cloudflare./ajax/libs/intl-tel-input/16.0.8/css/intlTelInput.css" />

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745439767a4627776.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信