Files
snippets-ts/lib/domUtils.ts
2023-03-05 02:27:15 +01:00

33 lines
744 B
TypeScript

export const addGlobalEventListener = (
type: string,
selector: string,
callback: (arg0: Event) => void,
options: boolean | AddEventListenerOptions,
parent = document
): void => {
parent.addEventListener(
type,
(e: Event) => {
if (e != null && e.target != null) {
const el = e.target as Element
if (el.matches(selector)) callback(e)
}
},
options
)
}
export const qs = (
selector: string | keyof HTMLElementTagNameMap,
parent = document
): Element => {
return parent.querySelector(selector) as unknown as Element
}
export const qsa = (
selector: string | keyof HTMLElementTagNameMap,
parent = document
): NodeListOf<Element> => {
return parent.querySelectorAll(selector)
}