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

202
Views
Keep dark or light mode over different pages with javascript

Basically, I got a dark mode on the front page, with the script being (from W3schools) :

<script>
function darklightmode() {
    var element = document.body;
    element.classList.toggle("dmode");
} </script>

And the button :

<button onclick="darklightmode()" style="background:none; border: none;">
                        <img src="images/ld-icon.png" class="icon">
                    </button>

and some CSS just for example :

.dmode li a{
transition: 1s all;
color: #2E3440;
background: none;}

So how can I, with some Javascript, make the mode the user is using stay between pages and not come back to default when accessing another page ?

Beginner here, any help appreciated.

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

0

You'd need to store the current theme somewhere. Try using localstorage.

Example from How do i set dark mode theme across multiple pages? (this question is a duplicate):


    checkbox.addEventListener( 'change', function() {
         localStorage.setItem('dark',this.checked);
         if(this.checked) {
              body.classList.add('dark')
         } else {
              body.classList.remove('dark')     
         }
    });

and this on each page:


    if(localStorage.getItem('dark')) {
         body.classList.add('dark');
    }
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!