mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2026-04-30 21:07:04 +02:00
[refactor] Moved 'clearEventListeners' to its own TS file
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
import { getElementById } from "./getElementById";
|
||||
|
||||
/**
|
||||
* Given an element by its ID, removes all event listeners from that element by cloning and
|
||||
* replacing. Then returns the new cloned element.
|
||||
* @param elemId The HTML ID to retrieve the element by.
|
||||
*/
|
||||
export function clearEventListeners(elemId: string) {
|
||||
try {
|
||||
const elem: HTMLElement = getElementById(elemId);
|
||||
const newElem: HTMLElement = elem.cloneNode(true) as HTMLElement;
|
||||
if (elem.parentNode !== null) {
|
||||
elem.parentNode.replaceChild(newElem, elem);
|
||||
}
|
||||
|
||||
return newElem;
|
||||
} catch (e) {
|
||||
// tslint:disable-next-line:no-console
|
||||
console.error(e);
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user