I have a dropdown Grade with values
["a", "b", "c"]
On selection change of dropdown, the values of Student dropdown should be loaded.
when "a" is selected, Dropdown students should populate ["Anna", "Hannah"]
When "b" is selected, Dropdown students should populate ["Billy", "Cathy"]
When "c" is selected, Dropdown students should populate ["Albert", "Deepti"]
Can we keep all the grades and students in single array(in js file)? If yes, how can I load only those values on selected index changed in javascript from the array ?
To add to the description, my code is in HTML, so my controls are:
<select id="grade" name="grade"></select>
<select id="student" name="student"></select>
Modified :
var grades_rank;
var grades_student;
var grades = { "very good": ["Anna", "Hannah"], "good":
["Billy", "Cathy"], "above average": ["Albert", "Deepti"] } ;
To load the dropdown:
for (var _i = 0; _i < grades.length; _i++) {
jQ('#Stu_Grades').append(jQ("<option></option>").attr("value", _i).text(grades[_i][grades_rank]));
}
Use an object:
When the dropdown value changes, use its new value to lookup the names you want to use:
When all is said and done, it might look something like the following: