(JS Interop) How do I remove an event listener from an element assigned via Jsinterop. #60955
Unanswered
GlacierFox
asked this question in
Q&A
Replies: 1 comment 1 reply
-
Hello @GlacierFox, export function nullifyKeyInput(el: Element, code: string, key: string) {
const element = el as HTMLElement;
// Remove old listener
const oldHandler = (element as any)._nullifyKeyHandler as (e: KeyboardEvent) => void;
if (oldHandler) element.removeEventListener("keydown", oldHandler);
// Create/store new handler
const handler = (e: KeyboardEvent) => {
if (e.code === code || e.key === key) e.preventDefault();
};
(element as any)._nullifyKeyHandler = handler;
element.addEventListener("keydown", handler);
}
export function removeNullifyKeyInput(el: Element) {
const element = el as HTMLElement;
const handler = (element as any)._nullifyKeyHandler as (e: KeyboardEvent) => void;
if (handler) element.removeEventListener("keydown", handler);
} Than, call |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
How do I remove an event listener after it's been added via JSInterop?
Here's the javascript (Typescript).
Of course I need a reference to the original event callback function to remove it but how and where would I store that? Is it actually possible?
I'm calling this every time a specific modal is loaded on a multi-use component and im assuming I'd be sort of stacking these event listeners if I don't remove them when I close the modal. (the component isn't being fully destroyed)
Bear with me I don't use javascript very often 😅
Beta Was this translation helpful? Give feedback.
All reactions