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 9223647
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:07:20+00:00 2026-06-18T04:07:20+00:00

I am using trying to use jQuery .change() to create two <select> elements whereby

  • 0

I am using trying to use jQuery .change() to create two <select> elements whereby the first <select id="state"> is for generating a list of states and the second <select id="city"> is to generate a list of cities.

The values of states and cities will not be hard-coded but generated from values pass from web services.

The list of options to be generated is supposed to work like this: if user selects state from <select id="state"> the selected value will be pass to a web service to retrieve the list to generate the <select id="city"> for cities.

I’m really not sure how to implement this. Can anyone please advise me?

  • 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-06-18T04:07:22+00:00Added an answer on June 18, 2026 at 4:07 am

    it should look something like

    $(function(){
    // populate the states list from Ajax 
    
    
        $.ajax( {
           url:"/listStates",
           type:"GET",
           success:function( data ) { 
                var statesList = data;
                if ( typeof(data) == "string" ){
                     statesList = JSON.parse(data);
                }
                $.each( statesList, function( index, state ){
                         $("#state").append($("<option></option>",{value:state.value, text:state.label});
                    });
           },
           error:function( result ) { 
                console.log(["error getting states",result]);
           }
        });
    
        // register on state list change
        $("#state").change( function(){
                // on change dispatch an AJAX request for cities and populate cities 
            $.ajax({ url : "/listCities",
                data : {"state": $("#state").val() },
                type:'GET',
                success:function( data ) {
                    var citiesList = data; // assuming list object
                    if ( typeof(data) == "string"){ // but if string
                        citiesList = JSON.parse( data );
                    } 
                                $("#city").empty();
                    $.each(citiesList, function(index,city){
                        $("#city").append($("<option></option>", {"value":city.value, "text":city.label}));
                    }
                },
                error:function( result ){ console.log(["error", result]); }
    
        })
    });
    

    This can get you started however I did not follow lint best practices here.

    Walk Through

    • I register for a “change” event on select with id state.
    • When a change if fired, I invoke an Ajax request to location “/listCities”
    • I assume this location is called with a “GET” method
    • I pass along the state that is currently selected – so the server will know which cities to list.
    • If the Ajax through an error, I log the error to the console.
    • If the Ajax was a success, I populate the select with id “city” with options containing values for each city and a label.

    I assumed the following things while writing the code

    • You have a route GET /listCities that expects a state.
    • The response from that route is a JSON containing a list of values and labels for each city. Something like this :

      [{ value : "AM" , label : "Amsterdam" }, .... ]

    The only things you may need to read about for this example are :

    • JQuery Ajax Calls
    • Best Practice To Populate A List With JQuery

    If you have any questions, please comment my response, I will be happy to explain/add/modify

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

Sidebar

Related Questions

I'm using (trying to use) jQuery to add a class to the first paragraph
I am using HMVC codeigniter. I am trying to use jquery ajax first time.
I am using ASP.Net and jQuery/jQuery UI and I am trying to use the
Hi I am trying to run an ajax query using jquery. If I use
Trying to use a php generating the email address using the ID eg: peopleID
So I'm trying to create a live-updating table using HTML and jQuery. Each row
I am trying to create a go-moku game using jquery,php, and mysql database. I
I trying to create an HTML/CSS/JavaScript based list manager. I'm using text input fields
I am trying to integrate jquery-chosen jquery-chosen into my application. I have two select
I'm trying to create a dialog window using JQuery. I'm making progress so far,

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.