added prototype
This commit is contained in:
21
webseite-gulp-ts/frontend/dev/scripts/utils/helpers.ts
Normal file
21
webseite-gulp-ts/frontend/dev/scripts/utils/helpers.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
// Generische Helferfunktion zum Selektieren von Element mit Überprüfung, ob das Element existiert
|
||||
export const querySelectorTyped = <T extends HTMLElement>(
|
||||
selector: string,
|
||||
parent: Element | Document = document,
|
||||
): T => {
|
||||
const element = parent.querySelector<T>(selector);
|
||||
if (!element) {
|
||||
throw new Error(`Element with selector "${selector}" not found.`);
|
||||
}
|
||||
return element;
|
||||
};
|
||||
|
||||
// Helferfunktion aus jQuery (youmightnotjquery)
|
||||
export const parents = (el: HTMLElement, selector?: string): HTMLElement[] => {
|
||||
const parents: HTMLElement[] = [];
|
||||
let currentEl: HTMLElement | null = el;
|
||||
while ((currentEl = currentEl.parentElement) && currentEl !== null && currentEl !== document.documentElement) {
|
||||
if (!selector || currentEl.matches(selector)) parents.push(currentEl as HTMLElement);
|
||||
}
|
||||
return parents;
|
||||
};
|
||||
Reference in New Issue
Block a user