I have two dropdown lists one of them shows a list of teams and the other one updates the values to show all the players from that team.
This is my current code:
// Creating list with all teams
var list1 = document.getElementById("list1");
for (var i = 0; i <= 19; i++) {
var a = document.createElement("OPTION");
a.setAttribute("value", data.teams[i].code);
var a1 = document.createTextNode(data.teams[i].name);
a.appendChild(a1);
list1.appendChild(a);
}
// Getting selected value of list 1
var option1 = list1.options[list1.selectedIndex].value;
// searching through json array and displaying only players from that team
data.elements.forEach(element => {
if(element.team_code == option1) {
var a = document.createElement("OPTION");
a.setAttribute("value", element.web_name);
var a1 = document.createTextNode(element.first_name + " " + element.second_name);
a.appendChild(a1);
players1.appendChild(a);
}
});
My code displays the correct players when selecting a team however if I select another value in the teams list it won't update the player list accordingly.
Any ideas how to do this?