I'm working on a website to contain my programming projects and such, that will grow as I get more skilled and make more things. I'm implementing a dark mode button that I would like to target all divs with the class "mainBody" however, it currently only targets the one that the dark button is in. I've tried many solutions from stackoverflow and other sites that don't seem to work. Here is the code that only changes one instance:
function goingDark() {
document.getElementsByClassName("mainBody")[0].style.backgroundColor = "#242526";
document.getElementsByClassName("mainBody")[0].style.color = "#ffffff";
document.getElementsByClassName("h2")[0].style.color = "#ffffff";
}
function goingLight() {
document.getElementsByClassName("mainBody")[0].style.backgroundColor = "#ffffff";
document.getElementsByClassName("mainBody")[0].style.color = "#000000";
document.getElementsByClassName("h2")[0].style.color = "#000000";
}
I'm pretty new to Javascript, so any help is greatly appreciated!