Hello I am trying to populate a select list using JSON and I am not getting any console errors however the list is not getting populated at all.
This is my html code
<select id="player_dropdown1">
</select>
This is my javascript code
var request = new XMLHttpRequest()
request.open('GET','https://cors-
anywhere.herokuapp.com/https://fantasy.premierleague.com/api/bootstrap-static/' , true)
request.onload = function() {
let dropdown = document.getElementById('player_dropdown1');
dropdown.length = 0;
let defaultOption = document.createElement('option');
defaultOption.text = 'Select a player';
dropdown.add(defaultOption);
dropdown.selectedIndex = 0;
var data = JSON.parse(this.response);
if (request.status >= 200 && request.status < 400) {
let option;
for (let i = 0; i < data.length; i++) {
option = document.createElement('option');
option.text = data.elements[i].firstname + data.elements[i].secondname;
option.value = data.elements[i].secondname;
dropdown.add(option);
}
} else {
}
console.log(data);
}
request.onerror = function() {
console.error('An error occurred fetching the JSON');
};
request.send();
I am not sure why the dropdown list is not getting populated. The json is getting parsed successfully to the console.