EmmaC
07-26-2010, 03:00 PM
Hi,
I am trying to use JavaScript for several linked drop down menus. The contents of the first drop down menu is defined in the code of the form, however, I do not know the best way to populate the following drop down menus depending on the selection of the previous.
I have something working with the following JavaScript code, however as there are lots of options I am not sure whether this is the best way of doing this.
<script type="text/javascript">
function setYear(chosen, selbox) {
selbox.options.length = 0;
if (chosen == " ") {
selbox.options[selbox.options.length] = new Option('Please select a year',' ');
setTimeout(setYear(' ',document.question.subject),5);
}
if (chosen == "1") {
selbox.options[selbox.options.length] = new
Option('exam1','11');
selbox.options[selbox.options.length] = new
Option('exam2','12');
selbox.options[selbox.options.length] = new
Option('exam3','13');
selbox.options[selbox.options.length] = new
Option('exam4','14');
selbox.options[selbox.options.length] = new
Option('exam5','15');
setTimeout(setYear('11',document.question.subject),5);
}
if (chosen == "2") {
selbox.options[selbox.options.length] = new
Option('exam6','twoone');
selbox.options[selbox.options.length] = new
Option('exam7','twotwo');
selbox.options[selbox.options.length] = new
Option('exam8','twothree');
selbox.options[selbox.options.length] = new
Option('exam8','twofour');
selbox.options[selbox.options.length] = new
Option('exam9','twofive');
selbox.options[selbox.options.length] = new
}
if (chosen == "3") {
selbox.options[selbox.options.length] = new
Option('exam10','threeone');
selbox.options[selbox.options.length] = new
Option('exam11','threetwo');
selbox.options[selbox.options.length] = new
Option('exam12','threethree');
selbox.options[selbox.options.length] = new
}
if (chosen == "11") {
selbox.options[selbox.options.length] = new
Option('subject1','111');
}
}
</script>
This is what I have in the form:
<td>Minimum DBS Year: </td><td colspan="2">
<select name="DBSyear" onchange="setYear(document.question.DBSyear.options
[document.question.DBSyear.selectedIndex].value,
document.question.exam);">
<option value=" " selected="selected"> </option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
</tr>
<tr>
<td>
Exam Paper:
</td>
<td>
<select name="exam" onchange="setYear(document.question.exam.options
[document.question.exam.selectedIndex].value,
document.question.subject);">
<option value=" " selected="selected">Please select a year</option>
</select>
</td>
</tr>
<tr>
<td>
Subject:
</td>
<td>
<select name="subject" size="1">
<option value=" " selected="selected">Please select an exam</option>
</select>
</td>
</tr>
How would I populate the drop down menus from tables in the database and filter the options depending on the previous drop down selection??
I would appreciate any help!
I am trying to use JavaScript for several linked drop down menus. The contents of the first drop down menu is defined in the code of the form, however, I do not know the best way to populate the following drop down menus depending on the selection of the previous.
I have something working with the following JavaScript code, however as there are lots of options I am not sure whether this is the best way of doing this.
<script type="text/javascript">
function setYear(chosen, selbox) {
selbox.options.length = 0;
if (chosen == " ") {
selbox.options[selbox.options.length] = new Option('Please select a year',' ');
setTimeout(setYear(' ',document.question.subject),5);
}
if (chosen == "1") {
selbox.options[selbox.options.length] = new
Option('exam1','11');
selbox.options[selbox.options.length] = new
Option('exam2','12');
selbox.options[selbox.options.length] = new
Option('exam3','13');
selbox.options[selbox.options.length] = new
Option('exam4','14');
selbox.options[selbox.options.length] = new
Option('exam5','15');
setTimeout(setYear('11',document.question.subject),5);
}
if (chosen == "2") {
selbox.options[selbox.options.length] = new
Option('exam6','twoone');
selbox.options[selbox.options.length] = new
Option('exam7','twotwo');
selbox.options[selbox.options.length] = new
Option('exam8','twothree');
selbox.options[selbox.options.length] = new
Option('exam8','twofour');
selbox.options[selbox.options.length] = new
Option('exam9','twofive');
selbox.options[selbox.options.length] = new
}
if (chosen == "3") {
selbox.options[selbox.options.length] = new
Option('exam10','threeone');
selbox.options[selbox.options.length] = new
Option('exam11','threetwo');
selbox.options[selbox.options.length] = new
Option('exam12','threethree');
selbox.options[selbox.options.length] = new
}
if (chosen == "11") {
selbox.options[selbox.options.length] = new
Option('subject1','111');
}
}
</script>
This is what I have in the form:
<td>Minimum DBS Year: </td><td colspan="2">
<select name="DBSyear" onchange="setYear(document.question.DBSyear.options
[document.question.DBSyear.selectedIndex].value,
document.question.exam);">
<option value=" " selected="selected"> </option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
</tr>
<tr>
<td>
Exam Paper:
</td>
<td>
<select name="exam" onchange="setYear(document.question.exam.options
[document.question.exam.selectedIndex].value,
document.question.subject);">
<option value=" " selected="selected">Please select a year</option>
</select>
</td>
</tr>
<tr>
<td>
Subject:
</td>
<td>
<select name="subject" size="1">
<option value=" " selected="selected">Please select an exam</option>
</select>
</td>
</tr>
How would I populate the drop down menus from tables in the database and filter the options depending on the previous drop down selection??
I would appreciate any help!