When I change item in selectbox, a property of DOM seems changed. So I coded below:
<select id="hoge">
<option value="0">hoge</option>
<option value="1">piyo</option>
</select>
<script>
var hoge = document.getElementById("hoge").addEventListener("change", func, false);
document.getElementById("hoge").value = 1; // <- (1)
function func(e) {
alert("Detected!");
}
</script>
In this case, when I change item in selectbox, func is called. But when (1) is called, func is not called. Change event seems to detect only committed by the user.
So I tried MutationObserver.
<select id="hoge">
<option value="0">hoge</option>
<option value="1">piyo</option>
</select>
<script>
var hoge = new MutationObserver(function(mutations) {
alert("Detected!");
});
hoge.observe(document.getElementById("hoge"), { attributes: true, subtree: true });
document.getElementById("hoge").options[1].setAttribute("selected", "selected"); // <- (1)
</script>
In this case, when (1) is called, func is called. But when I change item in selectbox, func is not called. MutationObserver seems to detect only attributes.
I want to call func in either case. How to I detect change in a property of DOM by script?