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

294
Vistas
Why am I getting a console error on addClass, but everything still works as expected?

This is a jQuery snippet intended to change the active class on a navigation item based on the scroll position. Everything works fine when tested, but I continue getting the error below.

Error:

Uncaught TypeError: Cannot read properties of undefined (reading 'addClass')

document.addEventListener("scroll", onScroll);

function onScroll(event) {
  let scrollPosition = $(document).scrollTop();
  let current;
  $("aside a").removeClass("clicked");
  $("aside a").each(function () {    
    let currentLink = $(this);
    let refElement = $(currentLink.attr("href"));
    if ( refElement.position().top+380 <= scrollPosition ) { 
      current = $(this);
    }
  });
  current.addClass("clicked");
}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

if ( refElement.position().top+380 <= scrollPosition ) { 

When the condition is false, current is undefined hence the error. If it still works as expected then you probably want to move the current.addClass("clicked"); inside the if statement.

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