I have the following elements, each one is the id of a select element which will have a dynamically generated dropdown list:
const year = document.getElementById('year');
const brand = document.getElementById('brand');
const version = document.getElementById('version');
const model = document.getElementById('model');
I have the following handle function which I would like to reuse for every single one of them:
function handleChange() {
let selected = year.options[year.selectedIndex].text;
console.log(selected);
}
year.addEventListener('change', handleChange);
So far that is working but idk how to make handleChange() take the id of the SELECT element. I tried the following code but it's not correct.
function handleChange(e) {
let id = e.target.id;
let selected = id.options[id.selectedIndex].text;
console.log(selected);
}
I'm trying to stay away from JQuery on this one.