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

197
Views
Dark Mode Function

I created a dark mode function that allows me to use local storage to "save" the last selected preference the user chose by clicking on an icon even if they go to different pages within the site, refresh, close, etc. What I can't figure out is how I can add an if statement so that the icon will also chance its "src" into a different png file when the "darkmode" is enabled and then switch back to another icon when it is disabled. If anyone could help me with this I would very much appreciate it.

let darkMode = localStorage.getItem('darkMode'); 

const darkModeToggle = document.querySelector('#dark-mode-toggle');

const enableDarkMode = () => {
  document.body.classList.add('darkmode');
  localStorage.setItem('darkMode', 'enabled');
}

const disableDarkMode = () => {
  document.body.classList.remove('darkmode');
  localStorage.setItem('darkMode', null);
}

if (darkMode === 'enabled') {
  enableDarkMode();
}


darkModeToggle.addEventListener('click', () => {
  darkMode = localStorage.getItem('darkMode');
  
  if (darkMode !== 'enabled') {
    enableDarkMode();

  } else {  
    disableDarkMode(); 
  }
});

const scrollBtn = document.getElementById("scroll_up")

    scrollBtn.addEventListener("click", () => {
        document.documentElement.scrollTop= 0;
    });
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Just modify the src attribute, like so:

const enableDarkMode = () => {
  document.body.classList.add('darkmode');
  localStorage.setItem('darkMode', 'enabled');
  darkModeToggle.src = "./images/darkToggle.svg"; // replace the path according to YOUR directory structure
}

const disableDarkMode = () => {
  document.body.classList.remove('darkmode');
  localStorage.setItem('darkMode', null);
  darkModeToggle.src = "./images/lightToggle.svg"; // replace the path according to YOUR directory structure
}
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!