Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

246
Visualizações
Checkbox classList Toggle

I'm trying to use a custom slider with an input type=checkbox to toggle between dark-mode and light-mode. It is not working with the javascript code I have.

var checkbox = document.getElementById('checkbox');
    checkbox.addEventListener('change', themeChange);
    
    function themeChange(){
      if (checkbox.checked == true){
        document.getElementById('body').classList.toggle('dark-mode');
      } else {
        document.getElementById('body').classList.toggle('light-mode');
      }
    }
.dark-mode {
      background-color: black;
      color: white;
    }
    
    .light-mode {
      background-color: white;
      color: black;
    }
<label class="switch">
       <input id="checkbox" type="checkbox" >
       <span class="slider round"></span>
    </label>

      <body id='body'>
    </body>

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

.toggle() will remove and add one single class. Try it this way instead

function themeChange(){
  if (checkBox.checked == true){
    document.getElementById('body').classList.add('dark-mode');
    document.getElementById('body').classList.remove('light-mode');
  } else {
    document.getElementById('body').classList.remove('dark-mode');
    document.getElementById('body').classList.add('light-mode');
}
about 4 years ago · Juan Pablo Isaza Relatório

0

toggle won't remove different the classes, you have to add and remove, please check the working code below.

var checkbox = document.getElementById('checkbox');
checkbox.addEventListener('change', themeChange);

function themeChange(){
  
  if (checkbox.checked == true){
    document.getElementById('body').classList.add('dark-mode');
    document.getElementById('body').classList.remove('light-mode');
  } else {
    document.getElementById('body').classList.add('light-mode');
    document.getElementById('body').classList.remove('dark-mode');
  }
} 
.dark-mode {
  background-color: black;
  color: white;
}

.light-mode {
  background-color: white;
  color: black;
}
<label class="switch">
   <input id="checkbox" type="checkbox" >
   <span class="slider round"></span>
</label>

<body id='body'>
    Theme switcher
</body>

about 4 years ago · Juan Pablo Isaza Relatório

0

Maybe this one?

    var checkbox = document.getElementById('checkbox');
    checkbox.addEventListener('change', ()=>{
      if (checkbox.checked == true){
        document.getElementById('body').classList.toggle('dark-mode',true);
        document.getElementById('body').classList.toggle('light-mode',false);
      } else {
        document.getElementById('body').classList.toggle('dark-mode',false);
        document.getElementById('body').classList.toggle('light-mode',true);
      }
    } )
    .dark-mode {
      background-color: black;
      color: white;
    }
    
    .light-mode {
      background-color: white;
      color: black;
    }
    <label class="switch">
       <input id="checkbox" type="checkbox" >
       <span class="slider round"></span>
    </label>

      <body id='body'>
    </body>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda