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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:00:09+00:00 2026-06-13T19:00:09+00:00

I have a simple form where a user can choose a colour and then

  • 0

I have a simple form where a user can choose a colour and then a thing from that colour category. The second select input updates based on the first input.

I have a jQuery script which allows me to target the first element based on id and change the next one accordingly when it’s id is specified. Now I want to make this into a reusable script so that I can choose ANY select and update the NEXT select accordingly. I can take care of the internal logic (url,GET parameter).

In my jQuery script: get parameter), but I can’t figure out how to target the next select element after the one selected (I tried with next() but it didn’t work).

$(document).ready(function () {
    var nextSelect = $('#id_colour');
    nextSelect.change(function () {
        function updateChoices() {
            var selected = nextSelect.val();
            if (selected) {
                // this line shown for completeness - will be refactored
                $.getJSON(url, {
                    colour_id: selected
                }, function (data, jqXHR) {
                    var options = [];
                    for (var i = 0; i < data.length; i++) {
                        output = '<option value="' + data[i].id + '"';
                        if (nextSelect.val() == data[i].id) {
                            output += ' selected="selected"';
                        }
                        output += '>' + data[i].name + '</option>';
                        options.push(output);
                    }
                    $('#id_thing').html(options.join(''));
                });
            }
        }
        updateChoices();
        $('#id_thing').change(updateChoices);
    });
});

Any help much appreciated.

EDIT
Here is the html. So I want that when one is changed the NEXT will be targeted. I cannot rely on ids or classes.

<form action="" method="post">
    <!-- colour -->
    <div class="form_div">
        <p><label for="colour">Colour:</label></p>
        <p>
            <select name="colour" id="id_colour">
                <option value="" selected="selected">---------</option>
                <option value="1">Green</option>
                <option value="2">Brown</option>
                <option value="3">Blue</option>
            </select>
        </p>
    </div>  
    <!-- thing -->
    <div class="form_div">
        <p><label for="thing">Thing:</label></p>
        <p>
            <select name="thing" id="id_thing">
                <option value="" selected="selected">---------</option>
                <option value="1">Branch</option>
                <option value="2">Leaf</option>
                <option value="3">Sky</option>
            </select>
        </p>
    </div> 

    <p><input type="submit" value="Submit Choice" /></p>
</form>
  • 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-13T19:00:12+00:00Added an answer on June 13, 2026 at 7:00 pm

    If you want to make reusable code (which may be used within other scripts), you can create a utility function:

    Just call nextSelect with three parameters; (1) the first select, (2) the second select which will change according to select1, (3) the url where you do the request.

    JQuery (v1.8.2):

    // Utility function
    $.nextSelect = function(selectOne, selectTwo, url) {
        var s1 = $(selectOne);
        s1.on('change', function() {
            var colourID = s1.val();
            if (colourID) {
                $.getJSON(url, {colour_id: colourID}, function(data, jqXHR){
                    $(selectTwo + ' option:selected').removeAttr('selected');
                    $(selectTwo + ' option[value="' + data[0].id + '"]').attr('selected', 'selected');
                });
                //Example of what the getJSON might do:
                $(selectTwo + ' option:selected').removeAttr('selected');
                $(selectTwo + ' option[value="' + 1 + '"]').attr('selected', 'selected');
                //End example. Remove/comment example when going live.
            }
        });        
    };
    //I have left the url empty for now,
    //$.nextSelect('#id_colour', '#id_thing', '');
    //If you allowed to at least select the container div:
    $mySelectedForm = $('.form_div').first();
    $.nextSelect("#" + $mySelectedForm.find('select').attr('id'), "#" + $mySelectedForm.next().find('select').attr('id'), '');
    

    The HTML (I just shortened it):

    <div class="form_div">
        <select name="colour" id="id_colour">
            <option value="" selected="selected">---------</option>
            <option value="1">Green</option>
            <option value="2">Brown</option>
            <option value="3">Blue</option>
        </select>
    </div>
    <div class="form_div">
        <select name="thing" id="id_thing">
            <option value="" selected="selected">---------</option>
            <option value="1">Branch</option>
            <option value="2">Leaf</option>
            <option value="3">Sky</option>
        </select>
    </div>
    

    ​
    Here is a fiddle

    If this was not what you were looking for, come with feedback and I will adjust the code.

    Cheers!

    EDIT:

    so that I can choose ANY select and update the NEXT select accordingly.

    Ok, so if I understood correctly you want to be able to select the first ‘select’ –> meaning you in some form need to hardcore the referencing of an id/class. This doesn’t need to be the select itself, it can be a wrapper class. The second select would be automatically selected.

    Modified the code and the fiddle to adjust for it.

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

Sidebar

Related Questions

I have the following simple form from which a user can select a state
I have a drop down menu that the user can select a location from.
I have a simple form where a user can add, edit, and delete people
On my homepage I have a simple search form. The user can enter some
I have a simple view function that's designed to allow the user to choose
I have a simple form that lists user names and for each user, the
I have an HTML page with a simple form. When the user clicks submit,
I have simple form. <form target=_blank action=somescript.php method=Post id=simpleForm> <input type=hidden name=url value=http://...> <input
I have a simple form that looks like this: <%= simple_form_for @study,:url => studies_path,
I have a simple form like this : <form method=post name=change_pass id=change_pass action=change_pass2.php> <input

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.