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

193
Vistas
Javascript Gallery: click button doesn't remove hide

My problem is when I click my buttons they doesn't remove the hide, it just work with the "all", also I tried to change getElementsByClassName() instead of querySelectorAll.

I have no idea what else to do. :(

I cannot show the HTML because it say I have too much code and need to add more details, but I'm pretty sure HTML is correct.

JS

let list = document.getElementsByClassName('list');
let itemBox = document.getElementsByClassName('itemBox');

for (let i = 0; i < list.length; i++) {
    list[i].addEventListener('click', function(){
        for (let j = 0; j < list.length; j++) {
            list[j].classList.remove('active');
        }
        this.classList.add('active');

        let dataFilter = this.getAttribute('data-filter');

        for (let k = 0; k < itemBox.length; k++) {
            itemBox[k].classList.remove('active');
            itemBox[k].classList.add('hide');
            
            if(itemBox[k].getAttribute('data-item') == dataFilter || dataFilter == "all"){
                itemBox[k].classList.remove('hide');
                itemBox[k].classList.add('active');
            }
        }
    });
}

I'm gonna cry, this is my first time here :')

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

0

Instead of using 2 class hide and active, how about simplify to using just 1 class active? Then you can use this.classList.toggle('active'), this will add 'active' if there is no active in the classList and remove 'active' if there is active in the classList.

https://www.w3schools.com/howto/howto_js_toggle_class.asp

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