Just structure your data like this:
var options = {
"IT": ['Programmer', 'Technician'],
"Management": ['Sales', 'Marketing']
};
.. then listen to the change
event and modify the target select
element accordingly.
In this case, the target select element's children option
elements are removed. Next, you would iterate over the selected object's property options[this.value]
and append option
elements:
Example Here
$('#choices').on('change', function () {
$('#dependent-select').empty();
options[this.value].forEach(function (value, index) {
$('#dependent-select').append($("<option />").val(value).text(value));
});
});
var options = {
"IT": ['Programmer', 'Technician'],
"Management": ['Sales', 'Marketing']
};
$('#choices').on('change', function () {
$('#dependent-select').empty();
options[this.value].forEach(function (value, index) {
$('#dependent-select').append($("<option />").val(value).text(value));
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="choices">
<option>IT</option>
<option>Management</option>
</select>
<select id="dependent-select">
</select>