A good way to approach your dilemma is using the classList API. I switch classes using nested for loops (for repeating instances of a class) and query the document like so:
var desiredElement = document.querySelectorAll('.light');
function switchTheme() {
for (var i = 0; i < desiredElement.length; i++) {
if (desiredElement[i].classList.contains('light')) {
desiredElement[i].classList.remove('light');
desiredElement[i].classList.add('dark');
} else if (desiredElement[i].classList.contains('dark')) {
desiredElement[i].classList.remove('dark');
desiredElement[i].classList.add('light');
}
}
}
By adding a button or hyperlink to either perform this function onclick or as href, I can toggle any element in the HTML document containing the light or dark class and effectively make a toggle switch to switch the sub design classes.
I used this to create a dark theme for a website that can toggle to a lighter theme by the user if desired.
var desiredElement = document.querySelectorAll('.light');
function switchTheme() {
for (var i = 0; i < desiredElement.length; i++) {
if (desiredElement[i].classList.contains('light')) {
desiredElement[i].classList.remove('light');
desiredElement[i].classList.add('dark');
} else if (desiredElement[i].classList.contains('dark')) {
desiredElement[i].classList.remove('dark');
desiredElement[i].classList.add('light');
}
}
}
.light {
background-color: #ddd;
}
.dark {
background-color: #333;
}
<DOCTYPE HTML>
<html>
<body class="light">
<button onclick="switchTheme()">Toggle</button>
</body>
</html>