How to Implement Pagination with Vanilla JavaScript


Pagination is used to break up a block of content into navigable pages on a webpage.

Aside from making it easier to see information, it also helps when loading data from a server. Using pagination, you can decide to only load a fixed number of items each time when the user decides to see them. This helps save time and avoid information overload on a page.

Here’s a sneak peak at the final product:

Our pagination element will use both page numbers and next page & previous page buttons for navigation.

Let’s get started!

1. Place the Content With HTML

In this demo, we’ll be placing the content to be paginated using HTML. In a real life example, we might be getting our content as JSON from a server so we’ll also take a look at how the pagination logic can be applied to this when we’re writing our JavaScript code.

First, we define our paginated content as a list:

We use the aria-live property so assistive technologies know when the content in this region has been updated and announce the new content to the reader. 

Next we define a nav element that contains next and previous buttons and the pagination numbers container:

We use the title attribute to display information in a tooltip and provide visual context for the button. The aria-label attribute is used by screen readers. 

The pagination-numbers div is empty for now as the page numbers will be determined using JavaScript.

2. Styling with CSS

We’re taking a fairly minimalistic approach for this demo so we’ll only style the pagination numbers and the container.

Place the pagination-container at the bottom of the page, override the default button styling for the pagination number and set an active class to show which page number is currently selected:

We’ve also added some simple :hover styling, but use the :not() selector to make sure it doesn’t apply to the disabled buttons.

3. Pagination Logic with JavaScript

Now to make it work. Let’s define what we’re trying to achieve with pagination:

  1. Only display a specific number of items on each page
  2. Display page numbers based on how many times the total items are broken down
  3. When a page number is clicked, change the display to that page
  4. Allow navigating to previous and next pages

First, get all the elements we’ll need:

Next, we’ll define our global variables:

  • paginationLimit: how many items we want displayed on each page; and
  • pageCount: how many pages there will be based on the paginationLimit.
  • currentPage: store the value of the currentPage

Calculate the pageCount by dividing the total number of items (listItems.length) by the paginationLimit and rounding to the highest whole number using the Math.ceil function.

“The Math.ceil() function always rounds a number up to the next largest integer.” – MDN

So if we have 50 items and we only want to display 10 items per page, our page count will be 50/10 = 5 pages. Likewise, if we have 55 items and we want to display 10 items per page, our page count will be 55/10 = 5.5 which rounds up to 6 pages.

Add Page Numbers

Now that we know how many pages we’ll need, we can define a function to create a new button for the page number and then add the buttons to the paginationNumbers container.

And then we’ll call the getPaginationNumbers function when the web page loads using the window.load() event:

Display Active Page

We want to define a function to only display as many items are allowed in the paginationLimit on each page. Here’s how we’ll implement it:

Set the value of the currentPage variable to the pageNum value:

Get the range for items to be shown. If we’re on page 1, we want to show items 1 to 10 (according to the paginationLimit). if we’re on page 2, we want to show items 11 to 20 and so on.

Loop through the list of items to be displayed and hide all the items. Then unhide all the items that fall within the range.

Note: since we’re working with arrays, item 1 will be at 0 and item 10 will be at position 9 so our logic looks like this:

We use this method since our implementation is just hiding HTML content. If we were trying to append content from a JSON object to the DOM, we could update the logic to look something like:

Update the window.load() event to set the current page as page 1 once the webpage loads:

Add Page Number Buttons Event Listener

Use a click event listener to trigger the setCurrentPage function whenever a page number button is clicked. Place this function inside the window.load() event so now the function looks like this:

Set Active Page Number

We also want to define a function to add the active class to the page number we just clicked. We reset all buttons on the page by removing the active class. Then, if the page-index of the button matches the currentPage global variable, we add the active class to that button. 

Include this function in the setCurrentPage function so the active page number is updated every time a new page is set:

At this point, we should have a functional Page navigation system that looks like this:

Next and Previous Buttons

Let’s extend this functionality to include the next and previous buttons. We can use the same implementation in the setCurrentPage() function, and update the logic:

  • for the previous button, we change the page using setCurrentPage(currentPage - 1)
  • for the next button, we change the page using setCurrentPage(currentPage + 1)

Include the event listeners for next and previous buttons in window.load():

Disable Page Navigation Buttons

We also want to include a disabling feature on the next and previous buttons. 

  • disable the previous button if we’re on the first page (if current page is 1)
  • disable the next button if we’re on the last page (if current page is total number of pages)

Finally, pass the handlePageButtonsStatus() function into the setCurrentPage() function so we check the navigation every time a new page is clicked:


And that’s all folks! We now have a fully functional and accessible implementation of a paginated section.

Level up Your Front End JavaScript Skills!

We have loads of great foundational and practical JavaScript tutorials on Tuts+, so you can learn as you create:

Source link

About Author


Please enter your comment!
Please enter your name here

Share post:




More like this

Frontend Rewind 2023 – Day 08

Explore Day 8 of our special calendar to...

20+ Best Procreate Color Palettes

Coming up with the perfect color combination...

10 Tips for Implementing Webflow On-page SEO — SitePoint

SEO is crucial for your website. After all,...