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

113
Views
JS page slider error after reaching second to last element in array

I've made a page slider which works to an extent. There are 3 div elements in the array. When advancing from the div at index 0 to the div at index 1, it works as it should. When advancing from the div at index 1 to index 2, I get an error: Uncaught TypeError: Cannot read properties of undefined (reading 'className') at HTMLAnchorElement.nextPage (eclipsesMain.js:17)

This is my code:

const pages = document.querySelector('.page-container').children;
const next = document.querySelector('.next');
const prev = document.querySelector('.prev');
let totalPages = pages.length;
let index = 0;

const nextPage = function () {
  if (pages[index] == totalPages) {
    index = 0;
  } else {
    index++;
  }
  console.log(pages);
  let selected = document.getElementsByClassName('active');
  selected[0].className = selected[0].className.replace('active', '');
  pages[index].className += 'active';
  console.log(pages[index]);
};

next.addEventListener('click', nextPage);
about 4 years ago · Juan Pablo Isaza
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!