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

292
Views
how to save toogle class with localstorage. so can someone check what's wrong with this code

if( localStorage.getItem("color") == "black" ) {
  
{
   var element = document.getElementById("body");
   element.classList.toggle("bdark");
}
{
   var element = document.getElementById("theader");
   element.classList.toggle("hdark");
}
{
   var element = document.getElementById("sh");
   element.classList.toggle("shh");
}
       
}

function myFunction() {
 
{
   var element = document.getElementById("body");
   element.classList.toggle("bdark");
}
{
   var element = document.getElementById("theader");
   element.classList.toggle("hdark");
}
{
   var element = document.getElementById("sh");
   element.classList.toggle("shh");
}
     var hs = document.getElementById("hs");
     
     var color;
     if(localStorage.getItem("color") == "black") {
       color = "black";
       
     localStorage.setItem("color",color)

}
.bdark {
  background-color: #333;
  color: white;
}

.hdark {
  background-color: black;
  color: white;
}

.shh {
  display: none;
}

.hs {
  display: none;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body id="body" class="light">

<p id="theader">Click the "Try it" button to toggle between adding and removing the "mystyle" class name of the DIV element:</p>

<button id="button" onclick="myFunction()">Try it</button>

<div id="aaas">
 <div id="sh" class="sh">&#9790;</div>
 <div id="hs" class="hs">&#9728;</div>
</div>

</body>
</html>

i want this code to onclick toggle class and when i refresh the page those toggled class remain same as they were before reloading the page with localstorage. so can someone check what's wrong with this code. help me with something similar/alternative to this one. thanks for reading this.

Details:-

i want this code to work as (onclick class change + saved with cokies/localstorage/or anything) so whenever i refresh or reopen the page it would be same class as it was when i left. or some alternative code that works same.

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

0

I fixed your code

if( localStorage.getItem("color") == "black" ) {

  {
     let element = document.getElementsByTagName("body");
     element.classList.toggle("bdark");
  }
  {
     let element = document.getElementById("theader");
     element.classList.toggle("hdark");
  }
  {
     let element = document.getElementById("sh");
     element.classList.toggle("shh");
  }

}

function myFunction() {

  {
     let element = document.getElementsByTagName("body");
     element.classList="bdark";
  }
  {
     let element = document.getElementById("theader");
     element.classList="hdark";
  }
  {
     let element = document.getElementById("sh");
     element.classList="shh";
  }
  {   
     let hs = document.getElementById("hs");
  }    
  let color;
  if(localStorage.getItem("color") != "black") {
    color = "black";
    localStorage.setItem("color", color)
  }
}
.bdark {
  background-color: #333;
  color: white;
}

.hdark {
  background-color: black;
  color: white;
}

.shh {
  display: none;
}

.hs {
  display: none;
}
<p id="theader">Click the "Try it" button to toggle between adding and removing the "mystyle" class name of the DIV element:</p>

<button id="button" onclick="myFunction()">Try it</button>

<div id="aaas">
 <div id="sh" class="sh">&#9790;</div>
 <div id="hs" class="hs">&#9728;</div>
</div>

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!