Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

144
Views
Add a button to change to dark mode in html website

I have added a button on my site which let's the users change to dark or light mode whenever they want. I added the button with a moon icon on it, but the problem is that I want that the moon icon changes to sun icon when the user is in dark mode. And changes to moon icon when user is in light mode.

function myfunction(e) {
  console.log("you clicked");
  document.documentElement.classList.toggle("dark-mode");
  document.querySelectorAll(".inverted").forEach((result) => {
    result.classList.toggle("invert");
  });
}
const btn = document.querySelector('.btn')
btn.addEventListener('click', myfunction);
.dark-mode {
  filter: invert(1) hue-rotate(180deg);
}

.invert {
  filter: invert(1) hue-rotate(180deg);
}
<button class="btn"><img src='moon.png'></img></button>

The .inverted class in js is because I don't want the images to invert their colors.. so I gave all the images a class='inverted'

So, this is what I've done and someone please let me know how I should change the icon to moon and sun depending on the current mode (light or dark)

Thanks!

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You could add the sun as another image to the button and change the visibility of the two images via your .dark-mode css class.

So whenever the .dark-mode class is present the moon gets hidden and the sun gets shown.

function myfunction(e) {
  console.log("you clicked");
  document.documentElement.classList.toggle("dark-mode");
  document.querySelectorAll(".inverted").forEach((result) => {
    result.classList.toggle("invert");
  });
}
const btn = document.querySelector('.btn')
btn.addEventListener('click', myfunction);
.dark-mode {
  filter: invert(1) hue-rotate(180deg);
}

.invert {
  filter: invert(1) hue-rotate(180deg);
}


/* button handling */

.moon {
  display: block;
}

.sun {
  display: none;
}

.dark-mode .moon {
  display: none;
}

.dark-mode .sun {
  display: block;
}
<button class="btn">
  <img class="moon" src="moon.png" alt="moon"></img>
  <img class="sun" src="sun.png" alt="sun"></img>
</button>

about 4 years ago · Juan Pablo Isaza Report

0

You could go with the CSS approach as in @Fabian's answer. If you would like to go with JS, you could simply use a flag to indicate whether or not the user switched to dark mode, and dynamically set the icon based on it.

let isDarkMode = document.documentElement.classList.contains("dark-mode");
function myfunction(e) {
    document.documentElement.classList.toggle("dark-mode");
    document.querySelectorAll(".inverted").forEach((result) => {
        result.classList.toggle("invert");
    });
    e.currentTarget.querySelector("img").src = isDarkMode ? "sun.png" : "moon.png";
 }
about 4 years ago · Juan Pablo Isaza Report

0

function myfunction(e) {
  const doc = document.documentElement
  doc.classList.toggle("dark-mode");

  document.querySelectorAll(".inverted").forEach((result) => {
    result.classList.toggle("invert");
  });



  const img = e.currentTarget.querySelector('img')
  const label = e.currentTarget.querySelector('span')
  if (doc.classList.contains('dark-mode')) {
    img.src = 'sun.png'
    label.innerHTML = 'Light mode'
  } else {
    img.src = 'moon.png'
    label.innerHTML = 'Dark mode'
  }

}
const btn = document.querySelector('.btn')
btn.addEventListener('click', myfunction);
.dark-mode {
  filter: invert(1) hue-rotate(180deg);
}

.invert {
  filter: invert(1) hue-rotate(180deg);
}

'
<button class="btn">
  <img src='moon.png' alt="" />
  <span>Dark mode</span>
</button>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!