-2
fetch("https://servicodados.ibge.gov.br/api/v1/localidades/estados")
.then( res => res.json() )
.then( states => {
for( const state of states ) {
ufSelect.innerHTML += `<option value="${state.sigla}">${state.sigla}</option>`
}
})
}
populateUFs()
function getCities(event) {
const citySelect = document.querySelector("[name=city]")
const stateInput = document.querySelector("[name=state]")
const ufValue = event.target.value
const indexOfSelectedState = event.target.selectedIndex
stateInput.value = event.target.options[indexOfSelectedState].text
const url = `https://servicodados.ibge.gov.br/api/v1/localidades/estados/${ufValue}/municipios`
citySelect.innerHTML = "<option value>Selecione a cidade</option>"
citySelect.disabled = true
fetch(url)
.then( res => res.json() )
.then( cities => {
for( const city of cities ) {
citySelect.innerHTML += `<option value="${city.nome}">${city.nome}</option>`
}
citySelect.disabled = false
})
}
document
.querySelector("select[name=uf]")
.addEventListener("change", getCities)
You inserted a lot of code into the question without need, it would be enough to show the format that is receiving the Api states and what you tried to do to sort!
– LeAndrade