I'm working on a project where I want to call a function whenever an option has been selected. Ideally, I would like to do something like this in my for loop but I have yet to find the correct event listener.
var s = document.getElementById("select");
for(var i = 0; i < 5; i++){
var o = document.createElement('option');
o.text = i;
o.setAttribute("value", i);
o.addEventListener("EVENT FOR BEING SELECTED", read);
s.add(o);
}
function read(){
console.log("Hello World!");
}
The purpose for this is to create a variable that keeps track of whatever option is selected. This would include the default selected option when the page has loaded which is why I didn’t want to use a click event listener.