Change select values based on radio selection

E

Eric B

Hi All:

I have a basic form with three radio buttons:

radio 1
radio 2
radio 3

When a particular radio is selected, I would like to populate a select
box with some predetermined values (variables). For ex..
if radio 1 is selected, value1, value2, value 3 and value 4
dynamically update the select.
if radio 2 is selected, value 5 dynamically updates the select.
if radio 3 is selected, value 7 dynamically updates the select.

Any ideas?

here is my snippet (not working of course, but I think I'm close..)

<HEAD>

<SCRIPT LANGUAGE="JavaScript">
selection = new Array(
new Array(
new Array("value1"),
new Array("value2"),
new Array("value3"),
new Array("value4"),
),
new Array(
new Array("value5"),
),
null,
new Array(
new Array("value6"),
)
);
function fillSelectFromArray(selectCtrl, itemArray, goodPrompt,
badPrompt, defaultItem) {
var i, j;
var prompt;
// empty existing items
for (i = radioCtrl.options.length; i >= 0; i--) {
radioCtrl.options = null;
}
prompt = (itemArray != null) ? goodPrompt : badPrompt;
if (prompt == null) {
j = 0;
}
else {
radioCtrl.options[0] = new Option(prompt);
j = 1;
}
if (itemArray != null) {
// add new items
for (i = 0; i < itemArray.length; i++) {
radioCtrl.options[j] = new Option(itemArray[0]);
if (itemArray[1] != null) {
radioCtrl.options[j].value = itemArray[1];
}
j++;
}
// select first item (prompt) for sub list
radioCtrl.options[0].selected = true;
}
}
// End -->
</script>

</HEAD>

<BODY>

<FORM NAME="main">
<input name="radio" type="radio" value="radio1"
onSelect="fillSelectFromArray(this.form.Team, ((this.selectedIndex ==
-1) ? null : selection[this.selectedIndex-1]));"> radio 1 <br>
<input name="radio" type="radio" value="radio2"
onSelect="fillSelectFromArray(this.form.Team, ((this.selectedIndex ==
-1) ? null : selection[this.selectedIndex1]));"> radio 2 <br>
<input name="radio" type="radio" value="radio3"
onSelect="fillSelectFromArray(this.form.Team, ((this.selectedIndex ==
-1) ? null : selection[this.selectedIndex2]));"> radio 3 <br>
<BR>
<SELECT NAME="Team" SIZE="4">
<OPTION> </OPTION>
<OPTION> </OPTION>
<OPTION> </OPTION>
<OPTION> </OPTION>
</SELECT>
</FORM>

Help is greatly appreciated.Thanks.

E
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top