<select id="city"></select>
<select id="warehouse"></select>
const citySelect = document.querySelector('#city');
const warehouseSelect = document.querySelector('#warehouse');
citySelect.addEventListener('change', function() {
updateSelectOptions(warehouseSelect, {
calledMethod: 'getWarehouses',
methodProperties: {
CityName: this.value,
},
});
});
updateSelectOptions(citySelect, {
calledMethod: 'getCities',
methodProperties: {
FindByString: '',
},
});
function updateSelectOptions(selectEl, queryData) {
fetch('https://api.novaposhta.ua/v2.0/json/', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
modelName: 'Address',
apiKey: '',
...queryData,
}),
})
.then(r => r.json())
.then(r => selectEl.innerHTML = r.data
.map(n => `<option>${n.Description}</option>`)
.join('')
);
}