---
title: "How do you implement pagination for a list of items using jQuery?"  
description: "How do you implement pagination for a list of items using jQuery?"  
author: "Revati S Misra"  
published: 2023-05-09  
updated: 2023-05-11  
canonical: https://www.mindstick.com/forum/158259/how-do-you-implement-pagination-for-a-list-of-items-using-jquery  
category: "jquery"  
tags: ["jquery ui", "jquery validate"]  
reading_time: 3 minutes  

---

# How do you implement pagination for a list of items using jQuery?

How do you [implement pagination](https://answers.mindstick.com/qa/116423/how-to-implement-pagination-in-api-with-sql) for a list of [items](https://www.mindstick.com/forum/33812/getting-number-of-items-selected-in-uicollectionview-in-ios) using jQuery?

## Replies

### Reply by Aryan Kumar

To implement [pagination](https://www.mindstick.com/blog/265/pagination-in-php) for a list of items using jQuery, you can follow these steps:

1. Divide the list of items into separate pages, each containing a certain number of items. You can do this by creating separate HTML elements for each page, such as divs or unordered lists.
2. Add navigation buttons or links to switch between the pages. These can be simple HTML links, buttons, or even custom designed UI elements.
3. Use jQuery to handle the navigation and display of the pages. This can be done by attaching click event handlers to the navigation buttons or links, and using jQuery's show() and hide() methods to display the appropriate page.

Here's an example code snippet to demonstrate the above steps:

```html
<div id="page1">
 <ul>
   <li>Item 1</li>
   <li>Item 2</li>
   <li>Item 3</li>
 </ul>
</div>
<div id="page2" style="display:none;">
 <ul>
   <li>Item 4</li>
   <li>Item 5</li>
   <li>Item 6</li>
 </ul>
</div>
<button id="prevBtn">Prev</button>
<button id="nextBtn">Next</button>
```

```javascript
$(document).ready(function() {
 // Set the number of items per page
 var itemsPerPage = 3;
 // Calculate the total number of pages
 var totalPages = Math.ceil($('li').length / itemsPerPage);
 // Set the initial page to display
 var currentPage = 1;
 // Handle the previous button click event
 $('#prevBtn').click(function() {
   if (currentPage > 1) {
     currentPage--;
     showPage(currentPage);
   }
 });
 // Handle the next button click event
 $('#nextBtn').click(function() {
   if (currentPage < totalPages) {
     currentPage++;
     showPage(currentPage);
   }
 });
 // Function to display the specified page and hide the others
 function showPage(pageNumber) {
   // Hide all pages except for the specified one
   $('div[id^="page"]').hide();
   $('#page' + pageNumber).show();
   // Update the active class on the navigation buttons
   $('.active').removeClass('active');
   $('#btn' + pageNumber).addClass('active');
 }
 // Initialize the page navigation
 for (var i = 1; i <= totalPages; i++) {
   // Add a navigation button for each page
   $('#nextBtn').before('<button id="btn' + i + '">' + i + '</button>');
   // Handle the navigation button click event
   $('#btn' + i).click(function() {
     currentPage = parseInt($(this).attr('id').substr(3));
     showPage(currentPage);
   });
 }
 // Show the initial page
 showPage(currentPage);
});
```

In this example, we start by defining the number of items per page and calculating the total number of pages based on the length of the list. We then add click event handlers to the previous and next buttons, and define a function to show the specified page and hide the others.

To initialize the page navigation, we loop through the total number of pages and add a navigation button for each one. We also attach a click event handler to each button to update the current page and show the corresponding page.

Finally, we show the initial page by calling the showPage() function with the currentPage variable.


---

Original Source: https://www.mindstick.com/forum/158259/how-do-you-implement-pagination-for-a-list-of-items-using-jquery

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
