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

238
Views
Casilla de verificación classList Toggle

Estoy tratando de usar un control deslizante personalizado con un tipo de entrada = casilla de verificación para alternar entre el modo oscuro y el modo claro. No funciona con el código javascript que tengo.

 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 answers
Answer question

0

.toggle() eliminará y agregará una sola clase. Pruébelo de esta manera en su lugar

 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 Report

0

toggle no eliminará diferentes clases, debe add y remove , verifique el código de trabajo a continuación.

 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 Report

0

¿Quizás este?

 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 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!