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

190
Views
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 answers
Answer question

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 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!