JSFiddle - React, Tailwind, and code Playground

by techiedelight

HTML

<div id="container" class="light"></div>
<button id="darkmode">Dark Mode</button>
<button id="lightmode">Light Mode</button>

CSS

div {
  width: 500px;
  height: 300px;
  border: 1px solid black;
}

button {
  margin: 10px;
}

.light {
  background-color: #ffffff;
}

.dark {
  background-color: #243447;
}

JavaScript

function replaceClass(id, oldClass, newClass) {
  var elem = $(`#${id}`);
  if (elem.hasClass(oldClass)) {
    elem.removeClass(oldClass);
  }
  elem.addClass(newClass);
}

$(document).ready(function() {
  $("#darkmode").click(function() {
    replaceClass("container", "light", "dark");
  });

  $("#lightmode").click(function() {
    replaceClass("container", "dark", "light");
  });
});