Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

92
Vistas
Window.onload function with arguments to show display: none img after refresh

I have a fav-icon on each card (carrousel of gifs) that is display and hidden by button click.And I need that if the users clicks the fav icon stays active even if the page refresh. I'm guiding from this question show display:none div after refresh but my code is a little bit different (Vanilla JS and Template literals):

Section creating the template for every card

const loadData = (data) => {
    let trendSection = document.querySelector('.card-container')
    let content = ''
    for (let i = 0; i < data.length; i++) {
        content += `
                    <img class="image-card" src="${data[i].images.downsized.url}">
                    <div class="custom-overlay-icons">
                        <a type="button"><img class="image_on" src="assets/images/icon-fav.svg" onClick="likeCard('${[i]}')"><img id="fav-active${[i]}" src="assets/images/icon-fav-active.svg"></a>
                       //The display of fav-active img is none by default in CSS
                    </div>`
    }
    trendSection.innerHTML = content
}

Section to get the icon to change on click

window.likeCard = (index) => {
let image = document.getElementById('fav-active' + i)
    if (!image.style.display || image.style.display === 'none') {
        image.style.display = 'block'
        localStorage.setItem('show');
    } else {
        image.style.display = 'none'
        localStorage.removeItem('show');
    }
}

Finnally, the problem, is that I dont know how and where to implement the function that the checks the state onLoad like they porpuse on the solution if I need the index variable, I got these:

window.onload = function(i) {
    let show = localStorage.getItem('show');
    if(show === 'true'){
         document.getElementById('fav-active' + i).style.display = "block";
    }
}

I don't know If I was clear but let me know if something does'nt make sense

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

From the MDN:

The setItem() method of the Storage interface, when passed a key name and value, will add that key to the given Storage object, or update that key's value if it already exists.

The Syntax:

storage.setItem(keyName, keyValue);

The Problem

You are trying to store the show variable in the localStorage, but you didn't pass any keyValue to it. so in onload event handler, the show variable is undefined.

The Solution:

set the show item properly in likeCard event handler:

if (!image.style.display || image.style.display === 'none') {
    image.style.display = 'block'
    localStorage.setItem('show', true);  // or false as your desire
} else {
    image.style.display = 'none'
    localStorage.removeItem('show', false); // or true as your desire
}

Optional

since the result of let show = localStorage.getItem('show') is only booleans (true/false), you can remove the comparison from the if block in the onload handler:

window.onload = function(event) {
    let show = localStorage.getItem('show');
    if(show) { // ------> if (true) / if(false)
         document.getElementById('fav-active').style.display = "block";
    }
}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda