In the snippet below, I'm trying to populate a list of states in a select dropdown using a fetch request. I'm able to console the list of states from a local json file.
I am able to console the list of states properly, but the select menu is not populating at all? I don't see any errors and I'm wondering where I went wrong?
Is there something wrong with my fetch request syntactically?
const states = document.getElementById('states');
states.innerHTML = '<option value="" disabled selected>Select State</option>';
fetch('https://jarednewnam.com/test/states.json')
.then(res => res.json())
.then(res => {
if (res.status) {
res.data.forEach(state => {
const option = document.createElement('option');
option.value = state.abbr;
let node = document.createTextNode(state.state)
option.appendChild(node);
states.appendChild(option);
});
}
});
<select name="states" id="states"></select>