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

160
Views
Set color theme with localStorage

I have a color theme system set up on my site. When you click a button, a class is added to <html> and its corresponding theme goes into effect. However, when I load my site for the first time (I use incognito mode to simulate this), none of the themes work. I want to rewrite a block of my code to basically say, "When there is nothing in localStorage, load 'theme-dark-blue'."

Here's my code:

  // function to set a given theme/color-scheme
  function setTheme(themeName) {
    localStorage.setItem('theme', themeName);
    document.documentElement.className = themeName;
  }

  // Immediately invoked function to set the theme on initial load
  (function () {
    if (localStorage.getItem('theme') === 'theme-dark-blue') {
      setTheme('theme-dark-blue');
    }
  })();

The other themes so far are 'theme-light-blue' and 'theme-dark-green'. I was thinking of trying to phrase it like, "If <html> does not have a class, set 'theme-dark-blue'"

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

0

  function setTheme(themeName) {
    localStorage.setItem('theme', themeName);
    document.documentElement.className = themeName;
  }

  // Immediately invoked function to set the theme on initial load
  (function () {
    if (localStorage.getItem('theme')) {
      setTheme(localStorage.getItem('theme'))
    } else {
      setTheme('theme-dark-blue')
    }

  })();

Here we check if the item in local storages exists, if it does we use it, if not we set de default theme class

about 4 years ago · Juan Pablo Isaza Report

0

Here's what we need to do

  1. Get the value from the localStorage
  2. Check that it is valid
  3. If the value is not valid or is missing, then use the default

const validTheme = ['theme-dark-blue', 'theme-dark-green'];

const getThemeFromLocalStorage = () => {
    const savedTheme = localStorage.getItem('theme');

    return validTheme.includes(savedTheme) ? savedTheme : null;
};

setTheme(getThemeFromLocalStorage() || 'theme-dark-blue');
about 4 years ago · Juan Pablo Isaza Report

0

Best guess.

So far as I know, if try you setItem() something that has been removed from local storage or does not exist, then the return response is null.

So maybe find a way to code: "If a class is added to my HTML, then create a setItem() with the same name as that new class and add it to localMemory." If the class exists in local memory, getItem() will confirm this. If it does not, getItem() will return null.

So create your if statement saying: "If getItem(className) = null, then set theme-dark-blue"

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!