I am veryyy new to this and am trying out a small project while I learn html/css/javascript. So, sorry if this is a fairly obvious question/answer.
Basically, I made my (very simple) site have a dark mode using this set up:
CSS
:root{
--primary-color: #ffffff;
--secondary-color: #1d1d1d;
}
.dark-theme{
--primary-color: #212121;
--secondary-color: #e0e0e0;
}
I then use primary/secondary for all my background/text colors. Then,
SCRIPT
var icon = document.getElementById("icon");
icon.onclick = function (){
document.body.classList.toggle("dark-theme");
if(document.body.classList.contains("dark-theme")){
icon.src = "/images/sun-2-svgrepo-com.png";
}else{
icon.src = "/images/moon-stars-svgrepo-com.png";
}
}
This is so when someone clicks my moon image tagged “icon” the colors inverse, and “icon” turns into a sun image. This works perfect for my dark mode!
I tried to insert the same process for “icon” underneath to see if it would happen at the onclick also…
var icon = document.getElementById("icon");
var icon = document.getElementById("icon");
icon.onclick = function (){
document.body.classList.toggle("dark-theme");
if(document.body.classList.contains("dark-theme")){
icon.src = "/images/sun-2-svgrepo-com.png";
icon2.src = "/images/lightmode.png";
}else{
icon.src = "/images/moon-stars-svgrepo-com.png";
icon2.src = "/images/darkmode.png";
}
}
it didn’t work.
My questions is, is there a way to change my other images (png, used as icons) from their light mode image to dark mode image. Basically, when the moon image changes to the sun image by the click, how can I have my other images switch as well?
Thank you!
