What I want is something like an onchange event with an html select element. But apparently the change event is for the options in a select. I need to detect when a new option is added.
I've searched google and SO and the links within but I don't want to use any frameworks.
I read up on creating events but I don't understand how to use the created event "onAdded". Like select.onAdded -> how does select know something (an option) is added?
Or is there a simpler way that I'm over looking? An event that I missed?
My point is, I want to do something like this
var select = document.getElementById("slct");
var btn = document.getElementById("btn");
btn.addEventListener("click", () => {
var option = document.createElement("option");
option.text = `${select.length} please work!`;
select.add(option );
//!!!select optionAdded event should have triggered!!!
})
function addPoint(){
//do things
}
/*
select.addEventListener("optionAdded", addPoint);
or
onSelectValueCountChanged(appropriateFunctionHere);
*/
<form>
<select id="slct" style="width: 300px" mutiple="true" size="11">
</select>
<button type="button" id="btn">click
</button>
</form>
Is it possible? In an easy to understand (I'm new to js) way without any frameworks?