In this post we will create a custom pager using prev and next functions in jQuery. You can treat this post as a simple demo of using prev and next functions. We will be using some HTML UI elements and we will apply the paging functionality by using native jQuery codes. In this way you can get the information about on which page is requested by the user, once you get it, you can create a jQuery ajax call and fetch the data from the database and show. You need to know the basics of jQuery and CSS before going further. I hope you will like this.
Background
We can find so many pager in on-line. Here in this post I am just trying to populate my own pager. Please be noted that this is just a demo, so that you cannot find more functionalities here.
Using the code
First thing you need to do is create a page.
- <!DOCTYPE html>
- <html>
- <head>
- <title>Prev Next In jQuery - Sibeesh Passion</title>
- </head>
- <body>
- <div id="container">
- <div id="outer">
- <div>-6</div>
- <div>-5</div>
- <div>-4</div>
- <div>-3</div>
- <div>-2</div>
- <div>-1</div>
- <div class="first">0</div>
- <div>1</div>
- <div>2</div>
- <div>3</div>
- <div>4</div>
- <div>5</div>
- <div>6</div>
- <div>7</div>
- </div>
- <div>
- <table>
- <tr>
- <td class="prev" title="Previous">
- <<<</td>
- <td class="next" title="Next">>>></td>
- </tr>
- </table>
- </div>
- </div>
- </body>
- </html>
- <style>
- #outer div {
- width: 20px;
- height: 20px;
- border: 1px solid #ccc;
- border-radius: 5px;
- padding: 10px;
- margin: 10px;
- box-shadow: 1px 1px 1px #999;
- font-style: oblique;
- text-align: center;
- float: left;
- background: green;
- }
- #outer .first {
- background: blue;
- }
- .prev,
- .next {
- font-weight: bold;
- font-size: 30px;
- padding: 10px;
- cursor: pointer;
- }
- #container {
- text-align: center;
- width: 50%;
- margin-left: 25%;
- }
- </style>
- <script>
- <script src="JQuery%20Versions/jquery-1.11.3.min.js"></script>

Now it is time to add our scripts.
- <script>
- $(document).ready(function ()
- {
- var $first = $(".first");
- var i = 0;
- $(".prev").click(function ()
- {
- ++i;
- if ($('#outer div').length / 2 < i)
- {
- i = 0;
- $('#outer .first').css('background', 'blue');
- $first = $(".first");
- }
- else
- {
- $first = $first.prev();
- $("#outer div").css("background", "green");
- $first.css("background", "blue");
- }
- });
- $(".next").click(function ()
- {
- ++i;
- if ($('#outer div').length / 2 < i)
- {
- i = 0;
- $('#outer .first').css('background', 'blue');
- $first = $(".first");
- }
- else
- {
- $first = $first.next();
- $("#outer div").css("background", "green");
- $first.css("background", "blue");
- }
- });
- });
- </script>
We are doing the above mentioned scenario both in prev click and next click. Now it is time for demo.
Please see the demo here: Custom Pager.
That is all. We did it.
Complete Code
- <!DOCTYPE html>
- <html>
- <head>
- <title>Prev Next In jQuery - Sibeesh Passion</title>
- <script src="http://sibeeshpassion.com/content/scripts/jquery-2.0.2.min.js"></script>
- <script>
- $(document).ready(function ()
- {
- var $first = $(".first");
- var i = 0;
- $(".prev").click(function ()
- {
- ++i;
- if ($('#outer div').length / 2 < i)
- {
- i = 0;
- $('#outer .first').css('background', 'blue');
- $first = $(".first");
- }
- else
- {
- $first = $first.prev();
- $("#outer div").css("background", "green");
- $first.css("background", "blue");
- }
- });
- $(".next").click(function ()
- {
- ++i;
- if ($('#outer div').length / 2 < i)
- {
- i = 0;
- $('#outer .first').css('background', 'blue');
- $first = $(".first");
- }
- else
- {
- $first = $first.next();
- $("#outer div").css("background", "green");
- $first.css("background", "blue");
- }
- });
- });
- </script>
- <style>
- #outer div {
- width: 20px;
- height: 20px;
- border: 1px solid #ccc;
- border-radius: 5px;
- padding: 10px;
- margin: 10px;
- box-shadow: 1px 1px 1px #999;
- font-style: oblique;
- text-align: center;
- float: left;
- background: green;
- }
- #outer .first {
- background: blue;
- }
- .prev,
- .next {
- font-weight: bold;
- font-size: 30px;
- padding: 10px;
- cursor: pointer;
- }
- #container {
- text-align: center;
- width: 50%;
- margin-left: 25%;
- }
- </style>
- </head>
- <body>
- <div id="container">
- <div id="outer">
- <div>-6</div>
- <div>-5</div>
- <div>-4</div>
- <div>-3</div>
- <div>-2</div>
- <div>-1</div>
- <div class="first">0</div>
- <div>1</div>
- <div>2</div>
- <div>3</div>
- <div>4</div>
- <div>5</div>
- <div>6</div>
- <div>7</div>
- </div>
- <div>
- <table>
- <tr>
- <td class="prev" title="Previous">
- <<<</td>
- <td class="next" title="Next">>>></td>
- </tr>
- </table>
- </div>
- </div>
- </body>
- </html>
Did I miss anything that you may think which is needed? Could you find this post as useful? I hope you liked this article. Please share me your valuable suggestions and feedback.
Your turn. What do you think?
If you have any questions, then please mention in the comments section.
Read this article in my blog here.

ankit shuklaPosted Sep 28, 2017, 1:25 AM
Please provide this article with dynamic data
Santhakumar MunuswamyPosted Oct 25, 2015, 11:26 AM
Good Article
Mukesh KumarPosted Oct 24, 2015, 12:04 PM
Good Job
Sibeesh VenuPosted Oct 24, 2015, 9:43 AM
Debendra Dash Thank you
Debendra DashPosted Oct 24, 2015, 5:06 AM
good one...
Sibeesh VenuPosted Oct 24, 2015, 3:28 AM
Pankaj Kumar Choudhary Thanks a lot for the information on achieving the same in different way. Much appreciated
Sibeesh VenuPosted Oct 24, 2015, 3:27 AM
Pankaj Kumar Choudhary Thanks a lot for noticing it. Will check it
Sibeesh VenuPosted Oct 24, 2015, 3:26 AM
Nitin Tyagi Thanks a lot
Sibeesh VenuPosted Oct 24, 2015, 3:26 AM
Harshad Pansuriya Thanks a lot
Sibeesh VenuPosted Oct 24, 2015, 3:26 AM
Gowtham K Thanks a lot
Nilesh JadavPosted Oct 24, 2015, 2:55 AM
Nice one sir !!
Pankaj Kumar ChoudharyPosted Oct 24, 2015, 1:02 AM
Some day ago i did same task for my friend so i provide jQuery code for custom pager. So it may be another way to achieve same goal using below code........... $(".prev").click(function () { $("#outer div").each(function (index, element) { if ($(this).css("background-color") == "rgb(0, 0, 255)") { if (index == 0) { $(this).css("background-color", "green"); $("#outer div").eq(7).css("background-color", "blue") } else { $(this).css("background-color", "green"); $(this).prev().css("background-color", "blue"); } } }) }) $(".next").click(function () { $("#outer div").each(function (index, element) { if ($(this).css("background-color") == "rgb(0, 0, 255)") { if (index == 13) { $(this).css("background-color", "green"); $("#outer div").eq(6).css("background-color", "blue") } else { $(this).css("background-color", "green"); $(this).next().css("background-color", "blue"); } return false; } }) })
Pankaj Kumar ChoudharyPosted Oct 24, 2015, 12:58 AM
Nice Article Sir........... I found some problem with your code that when count is reach at -6 and if we press the "<<<" button again then it take one more click to reach at node 0 and same as when count reach at 7 and now if we press ">>>" button then at same time 0 and 7 both become blue. So please check code once again...... it may be possible.........
NitinPosted Oct 24, 2015, 12:56 AM
Good one
Harshad PansuriyaPosted Oct 24, 2015, 12:01 AM
Nice one
Gowtham KPosted Oct 23, 2015, 10:06 PM
Good One