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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T14:33:49+00:00 2026-05-20T14:33:49+00:00

I have two select’s. The values/text of the second one will vary based on

  • 0

I have two select’s. The values/text of the second one will vary based on what is selected in the first select.

I will have onchange function linked to the first select. But I do not know

  • how I can save data for my onchange function in html
  • how I can change second select html definition (text/values + number of options) in my javascript code

Note that I don’t want to use any javascript framework

  • 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-20T14:33:50+00:00Added an answer on May 20, 2026 at 2:33 pm

    1) I assume you’re seeking advice on how to encode the data for the second select input. I suggest using JavaScript to store a mapping of possible values of the first select to value to be used by the second so you can index it directly, e.g.:

    // Assume "select1" can have option values "English" and "Spanish".
    var select2data = {
      'English': [['One', 1], ['Two', 2], ['Three', 3]],
      'Spanish': [['Uno', 1], ['Dos', 2], ['Tres', 3]]
    };
    

    This strategy is nice concise but requires programmatic generation of JavaScript. If you really want to use plain HTML then you could have a nested DIV structure and parse it as needed, e.g.:

    <div id="select2data" style="display:none">
      <div id="English">
        <div><div>One</div><div>1</div></div>
        <div><div>Two</div><div>2</div></div>
        ...
      <div id="Spanish">
      ...
    <script type="text/javascript">
      var s2d = document.getElementById("select2data")
        , select2data = {};
      for (var i=0; i<s2d.children.length; i++) { // Each of "id=English", etc.
        var data=s2d.children[i], opts=[];
        for (var j=0; j<data.children.length; j++) {
          opts.push(new Option(data.children[j].children[0].firstChild,
                               data.children[j].children[1].firstChild));
        }
        select2data[data.id] = opts;
      }
    </script>
    

    2) Now, setting the options of select2 can be done by looking up the data from the value of select1, encoding them as DOM Option objects, and adding them to select1:

    var sel1 = document.getElementById('select1')
      , sel2 = document.getElementById('select2');
    sel1.onchange = function() {
      var os = select2data[sel1.value]; // Get the options required by select1.
      if (os) {
        sel2.options.length = 0; // Clear the options for select2.
        for (var i=0; i<os.length; i++) {
          var o = new Option(os[i][0], os[i][1]);
          try { // Add each option, allowing for browser differences.
            sel2.add(o);
          } catch (ex) {
            sel2.add(o, null);
          }
        }
        sel2.selectedIndex = 0;
      }
      return true;
    };
    

    Note that this is all untested and has lots of room for improvement but should illustrate the idea.

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

Sidebar

Related Questions

I have two select boxes. One with list of Cities and the second one
I have two HTML select boxes. The first is called #1 and the second
I have two select elements. Making a choice on the first select will build
I have two select boxes, the second of which is populated once the first
I have two select fields in a form. Every time one of these select
I have two select elements, A and B: when A's selected option changes, B's
I have two select dropdown with same classname, I will like to return the
I have two select elements: hours and minutes. If one or both fields are
I have two select inputs set to display:none. Based on change() in another element
I have two select list that are equal how do i access the second

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.