JavaScript
DOM Manipulation
Dynamically create and manipulate DOM elements.
By EZ4Code Team
domcreate
Code
function h(tag, props = {}, children = []) {
const el = document.createElement(tag);
for (const [k, v] of Object.entries(props)) {
if (k === "class") el.className = v;
else if (k === "style") Object.assign(el.style, v);
else if (k.startsWith("on")) el.addEventListener(k.slice(2).toLowerCase(), v);
else el.setAttribute(k, v);
}
children.forEach(c => el.append(typeof c === "string" ? document.createTextNode(c) : c));
return el;
}Explanation
A hyperscript-like virtual DOM creation function.
More JavaScript Snippets
Array Map Filter Reduce
Chain map, filter, reduce, find, some, and every on arrays.
Array Deduplication
Deduplicate an array using Set.
Deep Clone
Deep clone objects, supporting common data types.
Debounce Function
Wait a period of time after an event triggers before executing; reset the timer if triggered again during the wait.
Throttle Function
Limit a function to execute at most once within a time interval.
Promise.all Concurrency Control
A Promise executor with concurrency limit.