Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

202
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda