I have the following code which populates a dropdown with a list of items contained in an array:
<form id="myGroupSelectForm">
<select id="selectGroup">
<option>Choose a Group</option>
</select>
<select id="selectStudent">
<option>Choose a Student</option>
</select>
</form>
<script type="text/javascript">
var select = document.getElementById("selectGroup");
var options = ["Group 1", "Group 2", "Group 3"];
var i;
for(i = 0; i < options.length; i++) {
var opt = options[i];
var el = document.createElement("option");
el.textContent = opt;
el.value = opt;
select.appendChild(el);
}
</script>
I have 3 more arrays, and each one contains lists of students. For example:
StudentList1 = ['student1', 'student2'...]
StudentList2 = ['student1', 'student2'...]
StudentList3 = ['student1', 'student2'...]
How would I dynamically populate my second dropdown list with one of these 3 arrays, depending on the selection made in the first dropdown list?
Is there a built in function that can check for the selection in the first dropdown? And if so, how can I capture the selection?
Thank you in advance.
<select>element, which listens to thechangeorinputevent. When the event is fired, you will want to purge all<option>elements in the second dropdown and populate it with the values you want.