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

101
Vistas
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 Respuestas
Responde la pregunta

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