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

206
Views
How to keep css style of element after reloading the web page in JS?

For example, I have some items whose status is completed and they have class completed

<body onload="loadItem()">
    <ol class="list-group">
        <li class="list-group-item completed" id="item-1">Text1</li>
        <li class="list-group-item completed" id="item-2">Text2</li>
    </ol>
</body>

Also, there are two buttons

<button type="button" onclick="showItem();" class="btn btn-outline-primary">Show completed</button>
<button type="button" onclick="hideItem();" class="btn btn-outline-primary">Hide completed</button>

And JavaScript functions

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');
            }
        }

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

But, it does not work. On page reload it only hides completed items. However, I want it to be shown as default and if I press Hide button, it should be hidden even after page reload

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

0

Instead of storing id's, you might be use this solution maybe? If I understand your question correctly, it might be help.

function loadItem() {
    if (localStorage.getItem('hide')) {
        hideItem()
    }
}

function showItem() {
    localStorage.removeItem('hide')
    document.querySelectorAll('.completed').forEach(li => li.style.display = 'list-item')
}

function hideItem() {
    localStorage.setItem('hide', true)
    document.querySelectorAll('.completed').forEach(li => li.style.display = 'none')
}
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!