Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

143
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda