Forum Moderators: open
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>Some Title</title>
<script type="text/javascript">
var FACULTIES = { //an object who's property names are names of Faculty divisions, and who's properties are arrays of professor names
'Applied Health Sciences' : ['Abdullah Abrahams', 'Clare Dorr', 'Louis Gascon', 'Ericka Trousdale'],
'Arts' : ['Ivette Mackin', 'Ayana Faison', 'Cristofer Rasco', 'Lesly Manke', 'Taj Patch', 'Emelia Otey'],
'Engineering' : ['Morgan Duan', 'Ramon Hoven', 'Ruth Nishimura', 'Campbell Mate'],
'Mathematics' : ['Jaslyn Aspinwall', 'Audrianna Draughn', 'Baileigh Hines', 'Willa Bouvier', 'Jaquelin Horsley'],
'Science' : ['Rocio Heiser', 'Coen Henderson', 'Kelly Crager', 'Sidney Lachman', 'Taylar Andrews']
};
//utility function used to add new Option to a <select> object
function addOption(select, option) {
try {
select.add(option, null); //standard method
} catch(er) {
select.add(option); //IE only
}
}
//utility function to empty out a <select> object
function emptySelect(select) {
for (var i = select.options.length - 1; i >= 0; i--) {
select.remove(select.options[i]);
}
}
//used by someForm.facultySelect's onchange event
function buildProfessorSelect(arrayOfProfessors) {
var i, profName,
len = arrayOfProfessors.length,
select = document.forms.someForm.professorSelect; //<-- edit "someForm.professorSelect" if you change name(s) of form and/or select>
addOption(select, new Option('Please Choose Professor'));
for (i = 0; i < len; i++) {
profName = arrayOfProfessors[i];
addOption(select, new Option(profName, profName));
}
}
//initiation/assignment of onchange event to facultySelect
window.onload = function () {
var el = document.forms.someForm.facultySelect; //<-- edit "someForm.facultySelect" if you change name(s) of form and/or select>
el.onchange = function () {
var value = this.options[this.options.selectedIndex].value;
emptySelect(this.form.professorSelect); //<-- edit "professorSelect" if you change name of select>
if (value && value in FACULTIES) {
//FACULTIES[value] will be one of the arrays of professor names from within the FACULTIES object
buildProfessorSelect(FACULTIES[value]);
}
};
};
</script>
</head>
<body>
<div>
<form name="someForm" action="" method="post">
<p>
<select name="facultySelect">
<option value="" selected="selected">Please Choose Faculty</option>
<option value="Applied Health Sciences">Applied Health Sciences</option>
<option value="Arts">Arts</option>
<option value="Engineering">Engineering</option>
<option value="Mathematics">Mathematics</option>
<option value="Science">Science</option>
</select>
<select name="professorSelect"></select>
</p>
</form>
</div>
</body>
</html>