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

356
Views
Uncaught TypeError: Cannot read properties of null (reading 'classList')
const sections = document.querySelectorAll('section[id]')

function scrollActive(){
    const scrollY = window.pageYOffset

    sections.forEach(current =>{
        const sectionHeight = current.offsetHeight,
              sectionTop = current.offsetTop - 58,
              sectionId = current.getAttribute('id')

        if(scrollY > sectionTop && scrollY <= sectionTop + sectionHeight){
             document.querySelector('.nav__menu a[href*=' + sectionId + ']').classList.add("active")
        }else{
            document.querySelector('.nav__menu a[href*=' + sectionId + ']').classList.remove("active")
        }
    })
}
window.addEventListener('scroll', scrollActive)
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

document.querySelector is not guaranteed to return an Element. If no matching element is found it will return null. (https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector).

So you're potentially saying: null.classList.remove(...)

Check that you've got an element from the querySelector before using it, e.g:

const element = document.querySelector('.nav__menu a[href*=' + sectionId + ']')

if (!element) return; // early return

if (scrollY > sectionTop && scrollY <= sectionTop + sectionHeight){
    element.classList.add("active")
} else {
    element.classList.remove("active")
}
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!