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

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

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