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

132
Views
Keeping my switch activated when go from a page to an other

I have started creating a personal website with a toggle switch and I am looking for a way to keep it activated even when I navigate to an other page in my website.

here is the JavaScript I used:

 btn.addEventListener('click', function () {
    document.body.classList.toggle('dark-theme');
})

Any help is much appreciated, thanks!

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

0

As it seems you want to use a dark and light theme on your website I can give you an example of how I solved this problem:

Javascript:

function changeTheme(){ //function to change Theme on button click
                        //function switches css class
    if(document.body.classList.contains('light')){
        document.body.classList.remove('light');
        document.body.classList.add('dark');
        document.cookie = "Color=dark;"
    }else{
        document.body.classList.remove('dark');
        document.body.classList.add('light');
        document.cookie = "Color=light;"
    }
}

$(document).ready(function() { //calls function every time your webpage loads and checks which theme is selected
    if(getCookie("Color")!= ""){
            if(getCookie("Color")=="dark"){
                changeTheme();
            }
        }
    
    });
function getCookie(cname) { //Helper function to check what theme is currently selected
    var name = cname + "=";
    var decodedCookie = decodeURIComponent(document.cookie);
    var ca = decodedCookie.split(';');
    for(var i = 0; i <ca.length; i++) {
      var c = ca[i];
      while (c.charAt(0) == ' ') {
        c = c.substring(1);
      }
      if (c.indexOf(name) == 0) {
        return c.substring(name.length, c.length);
      }
    }
    return "";
  }

CSS (here are the classes to change the css variables from light to dark and vice versa:

.light{
    --footer-background: rgb(189, 189, 189);
    --footer-textcolor: rgb(14, 14, 14);

    --body-backgroundcolor:rgb(255, 255, 255);

    --body-font-color: rgb(27, 27, 27);

    --scrollbar-track: rgb(192, 192, 192);
    --scrollbar-thumb: rgb(114, 114, 114);
}

.dark{
    --footer-background: rgb(59, 55, 55);
    --footer-textcolor: rgb(238, 238, 238);

    --body-backgroundcolor:rgb(17, 17, 17);

    --body-font-color: rgb(194, 194, 194);

    --scrollbar-track: rgb(39, 39, 39);
    --scrollbar-thumb: rgb(20, 20, 20);
}

Hope I could help

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!