function changeTheme() {
/* Naming convention for theme class: prefix "colorTheme"
followed by the input's value attribute */
var themeClassValue = "colorTheme" + this.value;
/* No action if clicking on the same input/label */
if (themeClassValue != document.body.dataset.currentThemeClass) {
document.body.classList.remove(document.body.dataset.currentThemeClass);
document.body.classList.add(themeClassValue);
/* Store new current theme value in custom data-current-theme-class attribute of body */
document.body.dataset.currentThemeClass = themeClassValue;
}
}
document.addEventListener('DOMContentLoaded', function() {
/* Execute code only when DOM content is loaded */
var selectedTheme = document.querySelector('input[id^="theme"]:checked');
if (selectedTheme) {
var themeClassValue = "colorTheme" + selectedTheme.value;
document.body.classList.add(themeClassValue);
document.body.dataset.currentThemeClass = themeClassValue;
} else {
/* If no input is checked, set data-current-theme-class to "null" */
document.body.dataset.currentThemeClass = null;
}
});
/* Select all inputs with id starting with "theme" */
var themeInputs = document.querySelectorAll('input[id^="theme"]');
for (let i = 0; i < themeInputs.length; i++) {
themeInputs[i].addEventListener("click", changeTheme);
}
html {
-webkit-box-sizing: border-box;
box-sizing: border-box;
font-size: 16px;
}
*,
*::after,
*::before {
-webkit-box-sizing: inherit;
box-sizing: inherit;
}
body {
margin: 0;
padding: 0;
min-height: 100vh;
width: 100vw;
border: 10px solid;
border-color: transparent;
-webkit-transition: all .4s;
transition: all .4s;
}
ul {
list-style: none;
margin: 0;
padding: 10px;
font-size: 20px;
}
li {
padding: 2px;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
input,
label {
cursor: pointer;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
outline: none;
}
.colorTheme1 {
color: rgb(36, 36, 33);
font-weight: 400;
background-color: rgb(248, 236, 220);
border-color: rgb(60, 75, 124);
}
.colorTheme2 {
background-color: rgb(39, 34, 28);
border-color: rgb(102, 102, 153);
color: rgb(248, 236, 220);
font-weight: 700;
}
.colorTheme3 {
background-color: rgb(255, 0, 0);
border-color: rgb(0, 255, 255);
color: rgb(255, 255, 0);
font-weight: 700;
}
<body>
<ul>
<li><input type="radio" id="theme1" name="theme" value="1" checked><label for="theme1">Light</label></li>
<li>
<input type="radio" id="theme2" name="theme" value="2"><label for="theme2">Dark</label></li>
<li>
<input type="radio" id="theme3" name="theme" value="3"><label for="theme3">Flashy</label></li>
</ul>
</body>