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

312
Views
Why my light theme resets after page is reloaded?

As you can see I store it in localStorage, it works fine. Problem comes when I reload the page, it is reseting back to the main (dark) theme..

let lightMode = localStorage.getItem("theme")

const lightModeToggle = document.querySelector(".moon-white");

const enableLightMode = () => {
    document.body.classList.add("light-theme");

    localStorage.setItem("theme", "enabled");
};

const disableLightMode = () => {
    document.body.classList.remove("light-theme");

    localStorage.setItem("theme", null);
};

lightModeToggle.addEventListener("click", () => {
    lightMode = localStorage.getItem("theme");
    if (lightMode !== 'enabled') {
        enableLightMode();
    } else {  
        disableLightMode ();
    }
});
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

I think you're missing document.onload listener to read the current theme from localStorage and then set it to the document when it gets loaded into DOM.

let lightMode = localStorage.getItem("theme")

const lightModeToggle = document.querySelector(".moon-white");

const enableLightMode = () => {
    document.body.classList.add("light-theme");

    localStorage.setItem("theme", "enabled");
};

const disableLightMode = () => {
    document.body.classList.remove("light-theme");

    localStorage.setItem("theme", null);
};

lightModeToggle.addEventListener("click", () => {
    lightMode = localStorage.getItem("theme");
    if (lightMode !== 'enabled') {
        enableLightMode();
    } else {  
        disableLightMode();
    }
});

document.addEventListener("load", () => {
    if (lightMode === 'enabled') {
        enableLightMode();
    } else {  
        disableLightMode();
    }
});

Update: Use onload to load event!

about 4 years ago · Juan Pablo Isaza Report

0

It works, but instead of using document.addEventListener("onload", () =>, I used:

window.addEventListener('load', (event) => {
lightMode = localStorage.getItem("theme"); 
    if (lightMode === 'enabled') {
        enableLightMode();
    } else {  
        disableLightMode();
    }
});
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!