theme-change-final.html
HTML document, ASCII text
1<!DOCTYPE html> 2<html data-theme="light"> 3 4<head> 5<style type="text/css"> 6html[data-theme=light] { 7background: #fff; 8color: #000; 9--buttonBackground: #fefefe; 10--buttonBorder: #ccc; 11--buttonColor: #001; 12} 13html[data-theme=dark] { 14background: #000; 15color: #fff; 16--buttonBackground: #101010; 17--buttonBorder: #333; 18--buttonColor: #fff; 19} 20html[data-theme=hotdogstand] { 21background: #f00; 22color: #fff; 23--buttonBackground: #ff0; 24--buttonBorder: #000; 25--buttonColor: #000; 26} 27button { 28background: var(--buttonBackground); 29border: var(--buttonBorder) solid 3px; 30color: var(--buttonColor); 31} 32</style> 33</head> 34 35<body> 36<button id="theme-switch">Switch Theme</button> 37This is epic! 38<script type="text/javascript"> 39const themes = ["light", "dark", "hotdogstand"]; 40const ThemeSwitchButton = document.querySelector("#theme-switch"); 41const HTML = document.documentElement; 42ThemeSwitchButton.addEventListener("click", SwitchTheme); 43function SwitchTheme() { 44let CurrentTheme = themes.indexOf(HTML.dataset.theme); // Gets how far in the current theme is into the "themes" constant. 45if (CurrentTheme + 1 >= themes.length) { // Checks if it's at the end of the array, 46HTML.dataset.theme = themes[0]; // If so, reset at the start. 47return; // Ends the function here, preventing next bit of code from running. 48} 49HTML.dataset.theme = themes[CurrentTheme + 1]; // Goes to the next theme. 50} 51</script> 52</body> 53 54</html> 55