One of my challenges involves working with two select elements. The first select allows for multiple options, while the second select is dependent on the choice made in the first one.
<select class="form-control" id="select1">
<option value="">Choose</option>
<option value="1">select1_option1</option>
<option value="2">select1_option2</option>
...
</select>
<select class="form-control" id="select2">
<option value="">Choose</option>
...
</select>
The options available in select2
are determined by the selected value in select1
.
To store this data in a compact format, I could use the following structure:
var select2data = {1:{11:"select2_option1",12:"select2_option2",13:"select2_option3",14:"select2_option4"},
2:{21:"select2_option1",22:"select2_option2"}};
For example, if the user selects value 1
in the first select, then select2
should display options as follows:
<select class="form-control" id="select2">
<option value="">Choose</option>
<option value="11">select2_option1</option>
<option value="12">select2_option2</option>
...
I am confident in updating the values in select2
, but I'm currently struggling to comprehend how to access and utilize the values stored in select2data
. You can view my progress in this jsfiddle demo.