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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:56:02+00:00 2026-06-14T04:56:02+00:00

I have a form that is suposed to help to user to choose a

  • 0

I have a form that is suposed to help to user to choose a specific thing at the end, but as the user fills the first options, the others below change. Something like this:

Type:
{
    t1:{
        Number of X:{
            1:{...}
            2:{...}
        }
        Number of Y:{...}
    }
    t2:{
        Number of X:{
            100:{...}
            200:{...}
        }
        Number of Y:{...}
    }
}

The user has the field Type with the options t1 and t2, when they choose t1, the field “Number of X” will be filled with 1 and 2, if they choose t2, the field “Number of X” will be filled with 100 and 200, and so on. Some of the choices depend on more than one field, its not straight down dependency (something like, if the user chooses “Number of X” = 100 then Foo is “A”, else, Foo can be “A”, “B” or “C”, but Foo is not bellow “Number of X”).

I tried a really naive implementation where I would set up event listeners on every field and see their changes, but eventually the code started growing out of control and I have a bunch of $("#foo").change(function(){...}); and its not imediatly obvious that the field listening to this is bar and not fbar.

I also tried JSON (as the example above), but there’s a lot of repetition, the deeper the tree grows and the number of possibilites increase, I have to write the same fields again and again. Sometimes choosing t1 will change an option directly even though its not directly bellow it, and even though it usually depends on another field entirely, and that’s more repetition in JSON.

How do I approach this problem? Is there a readable solution? Too much code is not the problem, as long as one can look at the code and understand the dependencies and their effects.

A code example (kinda like my code right now):

HTML:

<select id="type">
<option value=1>a</option>
<option value=2>b</option>
</select>
<select id="numOfX">
</select>
<select id="numOfY">
</select>

js:

$("#type").change(function()
{
    if($("#type").val() == 1)
    {
        $("#numOfX").append(new Option(1, "1", false, false));
        $("#numOfX").append(new Option(2, "2", false, false));
    }
    else if($("#type").val() == 2)
    {
        $("#numOfX").append(new Option(1, "100", false, false));
        $("#numOfX").append(new Option(2, "200", false, false));
    }
});

$("#numOfX").change(function()
{
    ...
});
  • 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-14T04:56:03+00:00Added an answer on June 14, 2026 at 4:56 am

    Update – Add example

    Have you try backbone.js library? It will make the Javascript code more manageable by adding models & structures. There is a learning curve though but it is really great. Once you learn Backbone, you can make use of the Backbone Forms plugin which will help in the dropdown management. Below is the demo link & sample code:

    Example 1

    $(function() {
    var cities = {
        'UK': ['London', 'Manchester', 'Brighton', 'Bristol'],
        'USA': ['London', 'Los Angeles', 'Austin', 'New York']
    };
    
    var subAreas = {
        'London' : ['L1', 'L2', 'L3', 'L4'],
        'Manchester' : ['M1', 'M2', 'M3', 'M4'],
        'Brighton' : ['B1', 'B2', 'B3', 'B4'],
        'Bristol' : ['BR1', 'BR2', 'BR3', 'BR4'],
        'Los Angeles' : ['LA1', 'LA2', 'LA3', 'LA4'],
        'Austin' : ['A1', 'A2', 'A3', 'A4'],
        'New York' : ['NY1', 'NY2', 'NY3', 'NY4']
    };
    
    
    //The form
    var form = new Backbone.Form({
        schema: {
            country: { type: 'Select', options: ['UK', 'USA'] },
            city: { type: 'Select', options: cities.UK },
            subArea: { type: 'Select', options: subAreas[cities.UK[0] ] }
        }
    }).render();
    
    form.on('country:change', function(form, countryEditor) {
        var country = countryEditor.getValue(),
            newOptions = cities[country];
    
        form.fields.city.editor.setOptions(newOptions);
    
        var city = newOptions[0],
            areaOptions = subAreas[city];
    
        form.fields.subArea.editor.setOptions(areaOptions);
    
    });
    
    form.on('city:change', function(form, cityEditor) {
        var city = cityEditor.getValue(),
            newOptions = subAreas[city];
    
        form.fields.subArea.editor.setOptions(newOptions);
    });
    
    //Add it to the page
    $('body').append(form.el);
    

    });​

    Example 2

    $(function() {
    var cities = {
        'UK': ['London', 'Manchester', 'Brighton', 'Bristol'],
        'USA': ['London', 'Los Angeles', 'Austin', 'New York']
    };
    
    var subAreas = {
        'UK.London' : ['L1', 'L2'],
        'USA.London' : ['L3', 'L4'],
        'UK.Manchester' : ['M1', 'M2', 'M3', 'M4'],
        'UK.Brighton' : ['B1', 'B2', 'B3', 'B4'],
        'UK.Bristol' : ['BR1', 'BR2', 'BR3', 'BR4'],
        'USA.Los Angeles' : ['LA1', 'LA2', 'LA3', 'LA4'],
        'USA.Austin' : ['A1', 'A2', 'A3', 'A4'],
        'USA.New York' : ['NY1', 'NY2', 'NY3', 'NY4']
    };
    
    var hashFunc = function(country, city){
        return country + "." + city;
    };
    
    
    //The form
    var form = new Backbone.Form({
        schema: {
            country: { type: 'Select', options: ['UK', 'USA'] },
            city: { type: 'Select', options: cities.UK },
            subArea: { type: 'Select', options: subAreas[ 'UK.London' ] }
        }
    }).render();
    
    form.on('country:change', function(form, countryEditor) {
        var country = countryEditor.getValue(),
            newOptions = cities[country];
    
        form.fields.city.editor.setOptions(newOptions);
    
        var city = newOptions[0],
            areaOptions = subAreas[hashFunc(country, city) ];
    
        form.fields.subArea.editor.setOptions(areaOptions);
    
    });
    
    form.on('city:change', function(form, cityEditor) {
    
        var city = cityEditor.getValue(),
            newOptions = subAreas[hashFunc(form.getValue().country, city)];
    
        form.fields.subArea.editor.setOptions(newOptions);
    });
    
    //Add it to the page
    $('body').append(form.el);
    });​
    

    As you also develop for mobile (probably Phonegap), you can also try ZeptoJS as an alternative for jQuery. It will improve the speed alot.

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

Sidebar

Related Questions

I have a form that the user submits and returns a result, but it
I currently have form that checks if a user has unsubmitted changes when they
I have a form that includes the first name and last name of a
I have a PictureBox control on a Form that is supposed to draw something
I have a form that I'm submitting through AJAX. The form includes many fields,
I have a form that I want to be validated before showing it initially.
I have a form that sends money value e.g <input type=text name=amount value=N50,000.00 NGN
I have a form that has label values that I would like to pass
I have a form that i want to appear at the top of every
I have a form that i want an administrator to fill out, then click

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.