From edf6d04137e26a0baba344505edf0ab4eb6d9930 Mon Sep 17 00:00:00 2001 From: DarkWiiPlayer Date: Wed, 18 Dec 2024 15:38:58 +0100 Subject: [PATCH] Add more events --- readme.md | 5 +++++ src/BetterSelect.js | 2 ++ 2 files changed, 7 insertions(+) diff --git a/readme.md b/readme.md index bd04c1f..4a3e852 100644 --- a/readme.md +++ b/readme.md @@ -21,3 +21,8 @@ ## Attributes * `closeSignal`: An AbortSignal that fires when the drop-down closes + +## Events + +* `change`: Fired whenever the value changes, even if via JavaScript +* `input`: Fired when the value is changed by selecting an option (after `change`) diff --git a/src/BetterSelect.js b/src/BetterSelect.js index 3b543f0..2d36704 100644 --- a/src/BetterSelect.js +++ b/src/BetterSelect.js @@ -203,6 +203,7 @@ export class BetterSelect extends HTMLElement { this.addEventListener("keypress", event => { if (event.key == "Enter") { this.selectDefault() + this.dispatchEvent(new InputEvent("input", {bubbles: true})) } }, {signal}) @@ -275,6 +276,7 @@ export class BetterSelect extends HTMLElement { */ setValue(value, state=value) { this.#value = {value, state} + this.dispatchEvent(new Event("change", {bubbles: true})); this.#internals.setFormValue(value, state) this.text.innerText = state }