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

299
Views
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 answers
Answer question

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