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

77
Views
Add a darkmode to your site via JavaScript

I'm a beginner and I set myself a challenge: create a darkmode for my portfolio with JS functions.

HTML :

<nav class="navbar dark-mode" id="navbar">

    <ul class="navbar_links">
        <li class="navbar_link first"><a href="#">Accueil</a></li>
        <li class="navbar_link second"><a href="#">Service</a></li>
        <li class="navbar_link third"><a href="#">Mission</a></li>
        <li class="navbar_link fourth"><a href="#">Portfolio</a></li>
        <li class="navbar_link fifth"><a href="#">Contact</a></li>
    </ul>

    <img id="lightModeIcon" src="img/light-mode.png" alt="">

</nav>

JAVASCRIPT:

    function lightMode() {

      const lightModeIcon = document.getElementById('lightModeIcon');
      const navbar = document.getElementById('navbar');

      if (navbar.classList.contains("dark-mode")) {
        lightModeIcon.addEventListener('click', () => {
          navbar.classList.replace("dark-mode", "light-mode");
          lightModeIcon.src = "img/dark-mode.png";
        })
    } else {
        lightModeIcon.addEventListener('click', () => {
          navbar.classList.replace("light-mode", "dark-mode");
          lightModeIcon.src = "img/light-mode.png";
        })
    }
}

     lightMode();

But I have a problem. I use "if...esle".

The "if" part works. But the "else" part doesn't work.

Normally, if the user has already clicked on the darkmode button, my #navbar doesn't contain the "dark-mode" class but ".light-mode". So the "else" part should run.

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

0

Think about when the code is running.

  • dark-mode is hard coded into the HTML
  • lightMode is called when the page loads.
  • The if statement binds one of the two event listeners

The condition for the else branch is never true when the function runs.


Write a single event listener, always bind it to the button, test what the current state is inside the event listener.


Note that you can detect the user's OS mode has pick your scheme based on that instead of having a completely separate UI to the rest of the user's system.

about 4 years ago · Juan Pablo Isaza Report

0

You're running this function once (presumably when the page is loaded), which will evaluate the condition once and register a single event listener (either the one that sets the class to dark mode, or the one that sets it to light mode). As such, clicks will do the same thing for the entirety.

Instead you should move your if-else check inside the event listener, such that you're checking the classes at the time of the click. This can be as simple as inverting the third and fourth lines of your function, e.g.:

const lightModeIcon = document.getElementById('lightModeIcon');
const navbar = document.getElementById('navbar');

// Always add this single event listener
lightModeIcon.addEventListener('click', () => {
   // Check the condition inside the listener, take action based on the current state
   if (navbar.classList.contains("dark-mode")) {
      navbar.classList.replace("dark-mode", "light-mode");
      lightModeIcon.src = "img/dark-mode.png";
   } else {
      navbar.classList.replace("light-mode", "dark-mode");
      lightModeIcon.src = "img/light-mode.png";
   }
});
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!