JavaScript - how to detect a programmatic value change on a select element (dropdown)

前端 未结 2 1460
小鲜肉
小鲜肉 2021-01-22 01:58

In JavaScript, I need to detect when the value of a certain select element has changed for any reason, including programmatically.

The onchange event i

2条回答
  •  孤街浪徒
    2021-01-22 02:32

    You can use the MutationObservers API to watch for changes to the DOM:

    https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver

    You might have to actually observe the option nodes for the selected attribute, instead of watching the parent select node, but it should be just as easy.

提交回复
热议问题