Index

JavaScript DOM framework

I tend to write a helper function for making a DOM element and these days it tend to end up looking kind of like this:

const elem = (tagName, props, ...children) => {
  const el = Object.assign(document.createElement(tagName), props);
  el.replaceChildren(...children);
  return el;
};

So uh we can use it for things. Beep boop:

const styles = `
.red {
  background-color: red;
}
.blue {
  background-color: blue;
}
.green {
  background-color: green;
}
`;
document.head.appendChild(elem("style", {}, styles));

outElement.appendChild(
  elem(
    "div",
    {},
    elem("p", { className: "red" }, "blue"),
    elem("p", { className: "blue" }, "red"),
    elem(
      "p",
      {
        onclick: (e) => e.target.classList.toggle("green"),
      },
      "green?"
    )
  )
);