Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6728329
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:09:26+00:00 2026-05-26T10:09:26+00:00

I have a list of countries and their area codes in a database. I

  • 0

I have a list of countries and their area codes in a database. I am trying to auto update a text field with the countries area code when a select option is selected.

ex.

<select name="country">
    <option value="CA" code="+1">Canada</option>
    ...
</select>

<input type="text" name="phone" />

I need to pass on to the script the value CA and the users need to see Canada. Basically what I’m asking is can I create a new option code and have jQuery update the text field that I choose to it.

Is this possible to do? or is there another way I can accomplish this?

Edit: “code” is supposed to be the country’s area code. So when a user selects Canada for example the text field phone gets updated with +1. I know I can just set value to +1 and then use .change() to update phone‘s .val() but I need the current value to pass on to the script that processes it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T10:09:27+00:00Added an answer on May 26, 2026 at 10:09 am

    I suggest you use an HTML5 data- attribute instead of a custom attribute:

    <select name="country">
        <option value="CA" data-code="+1">Canada</option>
        ...
    </select>
    
    <input type="text" name="phone" />
    

    and then bind a handler to the change event:

    $('select[name="country"]').change(function() {
        $('input[name="phone"]').val($(this).children('option:selected').data('code'));
    });
    

    or with less jQuery:

    $('select[name="country"]').change(function() {
        $('input[name="phone"]').val(this.options[this.selectedIndex].getAttribute('data-code'));
    });
    

    Another option would be to have a country -> code map in JavaScript

    var map = {
        'CA': '+1',
        ...
    };
    

    and then look up the code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this drop-down list of countries: <select name=country> <option value=>Country...</option> <option value=Afganistan>Afghanistan</option> <option
I have a list of countries in a database. I have created a select
I have a list of calling codes for all countries(the phone number prefixes), I
I have a list of data where I keep e.g. countries with code, name
I have a plain text file which have the list of countries as follows.
I simply have a table that contains a list of countries and their ISO
I have a list of countries, some with a very long name: <select name=countries>
I have a list of countries in this format in an external file: <?
I have a list called Countries, and each country has a list of Towns,
I have a list of cities, states/provinces, and countries and I need to find

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.