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

196
Views
How to save css style of element after refreshing the page in JavaScript?

For example, after clicking on Hide button some items disappear

function hideItem() {
            targList = document.getElementsByClassName("completed");
            document.getElementsByClassName("completed")
            if (targList) {
                for (var x = 0; x < targList.length; x++) {
                    targList[x].setAttribute('style', 'display: none !important');
                }
            }
            localStorage.setItem("autosave", targList);
        }

But after refreshing the page style of items do not save. How to preserve this style of element after reloading web page?

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

0

You can't store DOM into localStorage. You'd have to save identifying code for each element. For example, id.

Then do the opposite after loading the page.

function hideItem() {
  var targList = document.getElementsByClassName("completed");
  var hidden_ids = []
  for (var x = 0; x < targList.length; x++) {
    targList[x].setAttribute('style', 'display: none !important');
    hidden_ids.push(targList[x].id)
  }
  localStorage.setItem("autosave", JSON.stringify(hidden_ids));
}

function loadItem() {
  var hidden_ids = Json.parse(localStorage.getItem("autosave") || '[]');
  for (var x = 0; x < hidden_ids.length; x++) {
    document.getElementById(hidden_ids[x]).setAttribute('style', 'display: none !important');
  }
}
hideItem();
<div id="elem1" class="completed">hello</div>
<div id="elem2" class="not-completed">hello</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!