QuickSnip LogoA light blue logo with a thunderbolt in bracketsQuickSnip

Change Element Style

Changes the inline style of an element.

  • dom
  • style

Install

npx shadcn@latest add https://quicksnip.dev/r/change-element-style.json

This snippet is not verified yet. Review it before use, or help verify it.

Code

const changeElementStyle = (element, styleObj) => {
  Object.entries(styleObj).forEach(([property, value]) => {
    element.style[property] = value;
  });
};

Usage

const element = document.querySelector('.my-element');
changeElementStyle(element, { color: 'red', backgroundColor: 'yellow' });