I am struggling to make the code for two interdependent dropdown lists and I need some help:
// Question 1
var test1 = ['|Please Select','1|1','2|2','3|3']
// Question 2
var test2 = []
var test2a = ['|Please Select','1a|1a','2a|2a','3a|3a']
var test2b = ['|Please Select','1b|1b','2b|2b','3b|3b']
var test2c = ['|Please Select','1c|1c','2c|2c','3c|3c']
// Set the options from Question 1 using the values from the array test1
val = "test1";
function add_options(val){
arr = window[val];
var the_id = "#"+val;
$.each(arr, function(key,val){
options = val.split('|');
$(the_id).append(
$('<option></option>')
.val(options[0])
.html(options[1])
);
});
}
HERE I NEED HELP
When the user clicks on an option from test1, get the selected option.
Knowing the selected option, I could then build the array for question 2:
if(selected == "1"){
var test2 = test2a;
} else if (selected == "2") {
var test2 = test2b;
} else if (selected == "3") {
var test2 = test2c;
}
val = "test2";
function add_options(val){
arr = window[val];
var the_id = "#"+val;
$.each(arr, function(key,val){
options = val.split('|');
$(the_id).append(
$('<option></option>')
.val(options[0])
.html(options[1])
);
});
}
Hope you ca help!
function add_options(val)works correctly? And you want to know how to find which option was selected?