Skip to content

Instantly share code, notes, and snippets.

@rndme
Created March 20, 2026 05:46
Show Gist options
  • Select an option

  • Save rndme/470206822ca7ac9f3a3d249145f8eaf9 to your computer and use it in GitHub Desktop.

Select an option

Save rndme/470206822ca7ac9f3a3d249145f8eaf9 to your computer and use it in GitHub Desktop.
handy element creator
function Elm(tag = "div", contents = "", attrs = {}) {
var elm = tag instanceof Element ? tag : document.createElement(tag);
Object.entries(attrs).forEach(([k, v]) => {
if(typeof v === "function") return elm.addEventListener(k, v);
if(k === "dataset") return Object.keys(v).forEach(x => elm.dataset[x] = v[x]);
elm.setAttribute(k, v);
});
if(/</.test(contents)) { elm.innerHTML = contents; } else { elm.append(contents); }
return elm;
}//end Elm()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment