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

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

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