I'm trying to build a form with two select. Depending on the choice made in the first one, the list of possible choices in the second should be adapted.
So if select 1 choice value is 1 => Display object 1 as options in select 2.
I created this js code :
var EntiteGenerale = document.getElementById("TypeEntityId").value;
var select = document.getElementById("EntityId");
var index;
var Departement = {
1: "Finances et Administration",
2: "Service Logistique Transversale",
3: "Institut de Formation",
4: "Communication, Marketing & Commercial",
5: "Volontariat",
6: "Ressources Humaines",
7: "Service francophone du sang",
8: "Service ECM et DIH",
9: "Service Tracing",
10: "Département Secours"
};
var CentreSecours = {
1: "CG BUTGENBACH BULLINGEN",
2: "CS BLEGNY",
3: "CS BRABANT OUEST",
4: "CS CHARLEROI",
5: "CS HAUTE SENNE",
6: "CS HERSTAL - OUPEYE",
7: "CS TOURNAI",
8: "CS NAMUR",
9: "CS OTTIGNIES",
10: "CS OUGREE",
11: "CS PHILIPPEVILLE",
12: "CS ROCHEFORT",
13: "CS SPA",
14: "CS HESBAYE-CONDROZ",
15: "CS JODOIGNE",
16: "CS LIEGE",
17: "CS LUXEMBOURG",
18: "CS MONS",
19: "CS MOUSCRON"
};
function DisplayEntiteL() {
if (EntiteGenerale === 2) {
for (index in Departement) {
select.options[select.options.length] = new Option(Departement[index], index);
}
} else if (EntiteGenerale === 3) {
for (index in CentreSecours) {
select.options[select.options.length] = new Option(CentreSecours[index], index);
}
} else {
console.log("rien à afficher");
}
}
But when I try it, the else statement "rien à afficher" is always displaying because my options aren't added.
Could you tell me please what's wrong with my code ? See the full code in this JSFiddle.
Thank you in advance.
Best regards,
