I have a problem with this code. I have to load data in json format (var data2) and populate the options in select id = "selezionaVetro". The select items depends on what I choose in the select name = "Tipo_sistema". Seems doesn't work very well because If i change the first select, in the second select remains some previus options.
Thanks in advance!
var data2 = {
"sistemi": {
"scorr_vel_class_": {
"maxL": 110,
"maxH": 270,
"vetro": ["Trasparente", "Trasp satinato", "Stampa digitale"]
},
"scorr_vel_minimal_": {
"maxL": 110,
"maxH": 270,
"vetro": ["Trasparente", "Trasp satinato", "Extrachiaro"]
}
}
}
function loadLista() {
var select = document.getElementById("selezionaVetro");
var opzioni = data2.sistemi[document.getElementsByName("Tipo_sistema")[0].value].vetro;
for (var a = 0; a < select.options.length; a++) {
select.options[a].remove(a);
}
for (var i = 0; i < opzioni.length; i++) {
var opt = opzioni[i];
var el = document.createElement("option");
el.textContent = opt;
el.value = opt;
select.appendChild(el);
}
}
<select name="Tipo_sistema" class="col-6 custom-select mb-3" onchange="loadLista();">
<option selected value="scorr_vel_class_">Scorrevole a veletta classico</option>
<option value="scorr_vel_minimal_">Scorrevole a veletta minimal</option>
</select>
<select id="selezionaVetro">
<option>Choose a number</option>
</select>