2018-07-08 06:00:57 +02:00
|
|
|
import { isString } from "../helpers/isString";
|
|
|
|
import { getElementById } from "./getElementById";
|
|
|
|
|
2018-07-08 07:11:34 +02:00
|
|
|
/**
|
|
|
|
* Clears out all children from the provided element.
|
|
|
|
* If a string is passed in, it will treat it as an ID and search for the element to delete all children from.
|
|
|
|
* @param el The element or ID of an element to remove all children from.
|
|
|
|
*/
|
2021-05-01 09:17:31 +02:00
|
|
|
export function removeChildrenFromElement(el: string | null | Element): void {
|
2021-09-05 01:09:30 +02:00
|
|
|
if (el === null) {
|
|
|
|
return;
|
|
|
|
}
|
2018-07-08 06:00:57 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
try {
|
2021-09-09 05:47:34 +02:00
|
|
|
const elem: HTMLElement | Element = isString(el) ? getElementById(el as string) : (el as Element);
|
2018-07-08 06:00:57 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
if (elem instanceof Element) {
|
|
|
|
while (elem.firstChild !== null) {
|
|
|
|
elem.removeChild(elem.firstChild);
|
|
|
|
}
|
2018-07-08 06:00:57 +02:00
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
} catch (e) {
|
|
|
|
// tslint:disable-next-line:no-console
|
|
|
|
console.debug(e);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2018-07-08 06:00:57 +02:00
|
|
|
}
|