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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T23:27:06+00:00 2026-06-14T23:27:06+00:00

I have two <select> elements with different IDs. When the user selects a value

  • 0

I have two <select> elements with different IDs.

When the user selects a value from the first select box, I want the second select box to only display connected values.

My code:

<select id="ExtraField_1" name="ExtraField_1">
    <option value="1">test1</option>
    <option value="2">test2</option>
    <option value="3">test3</option>
    <option value="4">test4</option>
    <option value="5">test5</option>
    <option value="6">test6</option>
    <option value="7">test7</option>
    <option value="8">test8</option>
    <option value="9">test9</option>
    <option value="10">test10</option>
    <option value="11">test11</option>
    <option value="12">test12</option>
</select>
<select id="ExtraField_2" name="ExtraField_2">
    <option value="1">test1</option>
    <option value="2">test2</option>
    <option value="3">test3</option>
    <option value="4">test4</option>
    <option value="5">test5</option>
    <option value="6">test6</option>
    <option value="7">test7</option>
    <option value="8">test8</option>
    <option value="9">test9</option>
    <option value="10">test10</option>
    <option value="11">test11</option>
    <option value="12">test12</option>
    <option value="13">test13</option>
    <option value="14">test14</option>
    <option value="15">test15</option>
    <option value="16">test16</option>
    <option value="17">test17</option>
    <option value="18">test18</option>
    <option value="19">test19</option>
    <option value="20">test20</option>
</select>

So when user selects “test1” from first select boxm he will see only “test2”, “test3” and “test4” on the second select box; “test2” from first will show “test6”, “test7” and “test8” in the second box.

How can I use JavaScript to resolve this problem?

  • 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-14T23:27:08+00:00Added an answer on June 14, 2026 at 11:27 pm

    If you can use jQuery then you can always just clear and append the options to the second select.

        $('#ExtraField_1').change(function(){
            $('#ExtraField_2').find('option').remove()
            if($(this).val() == '1'){
               $('#ExtraField_2').append($("<option </option>").attr('value','2').text('test2'));
               $('#ExtraField_2').append($("<option></option>").attr('value','3').text('test3'));
               $('#ExtraField_2').append($("<option></option>").attr('value','4').text('test4'));
            }
            if($(this).val() == '2'){
               $('#ExtraField_2').append($("<option></option>").attr('value','5').text('test5'));
               $('#ExtraField_2').append($("<option></option>").attr('value','6').text('test6'));
               $('#ExtraField_2').append($("<option></option>").attr('value','7').text('test7'));
            } 
       });
    

    ​

    http://jsfiddle.net/8XVuv/2/

    using only javascript is a bit more complicated but I would still take the same approach.

        function createOption(otext,oValue){
            var newOption = document.createElement('option');
            newOption.text = otext;
            newOption.value = oValue;
            return newOption;
        }
    
        function clearSelect(theSelect){
            for(var i = 0;i <= theSelect.options.length+1;i++)
            {
                theSelect.remove();
            }
        }
    
        function onSelect(theSelect){
            var nextSelect = document.getElementById('ExtraField_2');
            clearSelect(nextSelect);
            var selected = theSelect.options[theSelect.selectedIndex];
    
            if(selected.value == 1){
                nextSelect.add(createOption('test2','2'));
                nextSelect.add(createOption('test3','3'));
                nextSelect.add(createOption('test4','4'));
            }
            if(selected.value == 2){
                nextSelect.add(createOption('test5','5'));
                nextSelect.add(createOption('test6','6'));
                nextSelect.add(createOption('test7','7'));
            }
    
        }
    

    with html:

    <select id="ExtraField_1" name="ExtraField_1" onchange="javascript: onSelect(this);" >
        <option value="0">Select a test..</option>
        <option value="1">test1</option>
        <option value="2">test2</option>
        <option value="3">test3</option>
        <option value="4">test4</option>
        <option value="5">test5</option>
        <option value="6">test6</option>
        <option value="7">test7</option>
        <option value="8">test8</option>
        <option value="9">test9</option>
        <option value="10">test10</option>
        <option value="11">test11</option>
        <option value="12">test12</option>
    </select>
    
    <select id="ExtraField_2" name="ExtraField_2">
        <option value="0">Select from the left</option>
    </select>​
    

    as you can see it still does what you expect but you are not hiding options.

    http://jsfiddle.net/upKzW/13/

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

Sidebar

Related Questions

I have two different elements with the same id . How do I select
I have a webpage with different elements (a list of links and two select
I have two select elements, A and B: when A's selected option changes, B's
OK, I have two HTML select elements and a button. I need to disable
I have a select element. <select class='cSelectType'> <option value=1>one</option> <option value=2>two</option> <option value=3>three</option> <option
So I have trouble trying to use any of these two elements for different
I have a scenario where a visitor can select a value from a drop-down
I have stumbled upon a problem. I have two different select fields. Each of
I have two select fields in a form. Every time one of these select
I have two select menus (#id1 and #id2) that, when validated as containing a

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.