I\'ve been trying to get some dynamic select functionality working, but despite many different tutorial i\'ve yet to get it to work. For ease of reading, i\'ve brought the c
This can be achieved with vanilla javascript only. Make sure that there is a route at companies/[id]/contacts.json
which returns correct data.
const select = document.querySelector('#company_id');
select.addEventListener('change',function(e) {
axios.get(`/companies/${e.target.value}/contacts.json`)
.then((res) => {
let contactSelect = document.querySelector('#contact_id')
contactSelect.innerHTML = ''
res.data.map((model, i) => {
contactSelect.options[i] = new Option(model.name, model.id);
})
})
});