From f2c5755d6e732915d82d75044001c66a565863d0 Mon Sep 17 00:00:00 2001 From: Alex Iglesias Date: Sun, 25 Apr 2021 19:59:30 +0200 Subject: [PATCH] Added removeChildElements helper --- helpers/index.ts | 1 + helpers/removeChildElements.ts | 11 +++++++++++ package.json | 2 +- 3 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 helpers/removeChildElements.ts diff --git a/helpers/index.ts b/helpers/index.ts index 9888c8e..099bd28 100644 --- a/helpers/index.ts +++ b/helpers/index.ts @@ -6,6 +6,7 @@ export { default as wait } from './wait'; export { default as throwError } from './throwError'; export { default as selectInputElement } from './selectInputElement'; export { default as queryElement } from './queryElement'; +export { default as removeChildElements } from './removeChildElements'; export { default as isVisible } from './isVisible'; export { default as isScrollable } from './isScrollable'; export { default as getObjectKeys } from './getObjectKeys'; diff --git a/helpers/removeChildElements.ts b/helpers/removeChildElements.ts new file mode 100644 index 0000000..b4110d0 --- /dev/null +++ b/helpers/removeChildElements.ts @@ -0,0 +1,11 @@ +/** + * Remove all child elements from a parent element + * @param element Parent element + * @param selector Optional: only remove the elements that match this selector + */ +const removeChildElements = (element: Element, selector?: string): void => { + const childElements = element.querySelectorAll(selector || '*'); + childElements.forEach((childElement) => childElement.remove()); +}; + +export default removeChildElements; diff --git a/package.json b/package.json index 8694c13..e8f204e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@finsweet/ts-utils", - "version": "0.1.1", + "version": "0.2.0", "description": "Typescript utils for custom Webflow projects.", "main": "index.ts", "module": "index.ts",