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

111
Views
Error when removing a class in the mobile version.Cannot read properties of undefined (reading 'classList');

I have some div elements that have a '.swiper-slide' class. I need this class to be removed on the mobile version of the screen. However, I am getting the error. Cannot read properties of undefined (reading 'classList')Please tell me how to fix it My js:

function overlay() {
        let swiperSlide = document.querySelectorAll('.swiper-slide');
        if (window.innerWidth < 991) {
            swiperSlide.forEach((swip, index) => {
                swip[index].classList.remove('swiper-slide');
            });
        } else {
           swiperSlide.forEach((swip, index) => {
                swip[index].classList.add('swiper-slide');
            });

        }
    };
    overlay();
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

This is wrong:

swip[index].classList.remove('swiper-slide');

In the callback to forEach, the first argument (in this case swip) isn't a copy of the array over which you're iterating. It's the individual element for any given iteration over the list. So you'd access it directly:

swip.classList.remove('swiper-slide');

If you wanted to use the index (the second argument to the callback) then you'd use that on the array, not the element:

swiperSlide[index].classList.remove('swiper-slide');
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!