Created
May 31, 2023 07:03
-
-
Save Lawondyss/6876ba4d3cb517181f87b043ee6f09ba to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function qs(selector: string, parent: ParentNode = document): Element | null { | |
return parent.querySelector(selector) | |
} | |
function qsa(selector: string, parent: ParentNode = document): Element[] { | |
return [...parent.querySelectorAll(selector)] | |
} | |
function createElement(type: string, options: Record<string, string | object> & Partial<{ class: string, dataset: object, text: string }> = {}): HTMLElement { | |
const elm = document.createElement(type) | |
if (options.class) { | |
elm.classList.add(...options.class.split(' ')) | |
delete options.class | |
} | |
if (options.text) { | |
elm.textContent = options.text | |
delete options.text | |
} | |
if (options.dataset) { | |
Object.entries(options.dataset).forEach(([key, value]) => elm.dataset[key] = value) | |
delete options.dataset | |
} | |
Object.entries(options).forEach(([key, value]) => elm.setAttribute(key, value as string)) | |
return elm | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment