/    Sign up×
Community /Pin to ProfileBookmark

Controlling contents of drop-down boxes with radio buttons

Hi

I want to write a script (JavaScript) that will control what options are displayed in a drop-down box depending on what radio button a user selects eg If the user selects radio button 1 then A,B & C are options, if the user selects radio button 2 then D, E and F are options and if a user selects radio button 3 then G,H and I are options.

I have written a script to deal with the situation where if a user selects an option from a first drop-down box then different options are given in a second drop-down box but have not been able to successfully adapt to it the case where a set of radio buttons are used instead of the first drop-down box.

Can someone please help me?

That script is below (with cuisines and dishes being used as options rather than numbers and letters)

var cuisines = new Array();
var cuisineField;
var dishField;

function initDishes()
{

//find the required fields – this should run on load of the document so isn’t written in a function.

cuisineField=null;
for (var x=0;x<entryForms.length && cuisineField==null;x++)
{
cuisineField = entryForms[x][“cuisineName”];
dishField = entryForms[x][“dishName”];
}

// **************************************************


**************************************

//ENTER THE DATA FOR THE DISHES HERE

//in alphabetical order first by Cuisine, then by Dish Name
addDish(“Chinese”,”Cantonese Roasted Duck”);
addDish(“Chinese”,”Red Cooked Pork with Tofu”);
addDish(“Chinese”,”Mongolian Beef Fire Pot”);
addDish(“Italian”,”Fegati Alla Veneziana”);
addDish(“Italian”,”Abaccio Al Forno”);
addDish(“Italian”,”Maccheroni con le sarde”);
addDish(“Mexican”,”Chicken Enchilada”);
addDish(“Mexican”,”Quesadilla Casserole”);
addDish(“Mexican”,”Chiles Rellenos”);

//END ENTERING DATA FOR THE DISHES
// **************************************************


**************************************

//load the selections into the region field

cuisineField.options[0] = new Option(“-SELECT-“);
i = 0;
for(x in cuisines)
{
i++;
cuisineField.options[i] = new Option(x);
}
cuisineField.options[0].selected = true;
dishField.options[0].selected = true;

getCookies();
updateDishList();
getCookies();
}

function updateDishList()
{
//remove the current options in the list
for(j=dishField.options.length;j>=0;j–)
{
dishField.options[j] = null;
}

//populate the new options in the list
selCuisine = cuisineField.options[cuisineField.selectedIndex].text;
dishField.options[0] = new Option(“-SELECT-“);
dishField.options[0].selected = true;
i = 0;
for(x in cuisines[selCuisine]){
i++;
dishField.options[i] = new Option(x);
}
}

function addDish(cuisine, dish, id)
{
//define a new Dish
if(cuisines[cuisine]==undefined)
{
cuisines[cuisine] = new Array();
}
cuisines[cuisine][dish] = id;
}

to post a comment
Full-stack Developer

0Be the first to comment 😎

×

Success!

Help @mtkirwan spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.13,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...