theme-switcher.js 561 B

12345678910111213141516
  1. const darkStylesheet = document.getElementById("dark-theme");
  2. const themeSwitcherButton = document.createElement("input");
  3. themeSwitcherButton.type = "checkbox";
  4. themeSwitcherButton.onclick = () => {
  5. if (darkStylesheet.getAttribute('media') === 'none') {
  6. darkStylesheet.setAttribute('media', 'all')
  7. } else {
  8. darkStylesheet.setAttribute('media', 'none');
  9. }
  10. }
  11. const label = document.createElement("label");
  12. label.innerText = "Dark Theme";
  13. label.appendChild(themeSwitcherButton);
  14. document.body.getElementsByTagName("header")[0].appendChild(label);