[refactor] Moved 'clearEventListeners' to its own TS file

This commit is contained in:
Steven Evans
2018-07-07 23:19:19 -04:00
parent 7edf5b5f1a
commit 8016321bfb
17 changed files with 41 additions and 31 deletions
+23
View File
@@ -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;
}
}