By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
rocoderesrocoderes
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Notification Show More
Latest News
How to set the dropdown value by clicking on a table row
Javascript – How to set the dropdown value by clicking on a table row
JavaScript
Attempting to increase the counter, when the object's tag exist
Javascript – Attempting to increase the counter, when the object’s tag exist
JavaScript
Cycle2 JS center active slide
Javascript – Cycle2 JS center active slide
JavaScript
Can import all THREE.js post processing modules as ES6 modules except OutputPass
Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass
JavaScript
How to return closest match for an array in Google Sheets Appscript
Javascript – How to return closest match for an array in Google Sheets Appscript
JavaScript
Aa
Aa
rocoderesrocoderes
Search
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Follow US
High Quality Design Resources for Free.
rocoderes > JavaScript > Custom Pagination in JavaScript(beginners)
JavaScript

Custom Pagination in JavaScript(beginners)

Admin
Last updated: 2021/09/14 at 4:46 AM
Admin
Share
5 Min Read
custom pagination in javascript thumb

In this article, we will learn how to make Custom Pagination using Javascript. To make this first of all we need basic knowledge of HTML & CSS and Javascript. So in javascript, we need to familiar with function and DOM elements. So we can easily create custom pagination.

Contents
Custom Pagination in Javascript📂 index.html📂 style.css📂 index.jsDisplay Itemscreate Pagination ButtonSet The Pagination
custom pagination in javascript

Custom Pagination in Javascript

👉 So let’s start. First, we have to make the design using HTML & CSS. In this Project our main focus on javascript. Here the HTML code.

📂 index.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Users</title>

    <link rel="stylesheet" href="style.css" />
</head>
<body>
    
    <main>
        <div class="list" id="list"></div>
        <div class="pagenumbers" id="pagination"></div>
    </main>
    <script src="index.js"></script>
</body>
</html>
📂 style.css
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    font-family: sans-serif;
}

body {
    background: #F3F3F3;
}



main {
    display: flex;
    flex-direction: column;
    align-items: center;
}

main .list {
    width: 100%;
    max-width: 768px;
    background-color: #FFF;
    border: 1px solid #CCC;
    margin-top: 25px;
}

main .list .item {
    padding: 15px;
    border-bottom: 1px solid #CCC;
}
main .list .item:last-of-type {
    border-bottom: none;
}
main .list .item:hover {
    background: rgba(0, 0, 0, 0.05);
}

.pagenumbers {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    align-items: center;
}

.pagenumbers button {
    width: 50px;
    height: 50px;

    appearance: none;
    border: none;
    outline: none;
    cursor: pointer;

    background-color: #44AAEE;

    margin: 5px;
    transition: 0.4s;

    color: #FFF;
    font-size: 18px;
    text-shadow: 0px 0px 4px rgba(0, 0, 0, 0.2);
    box-shadow: 0px 0px 4px rgba(0, 0, 0, 0.2);
}

.pagenumbers button:hover {
    background-color: #44EEAA;
}

.pagenumbers button.active {
    background-color: #44EEAA;
    box-shadow: inset 0px 0px 4px rgba(0, 0, 0, 0.2);
}

👉 Now we come to the javascript portion. First, we declare the array of items.

custom pagination in javascript -arrays
📂 index.js
const listItems = [
    'Item 1',
    'Item 2',
    'Item 3',
    'Item 4',
    'Item 5',
    'Item 6',
    'Item 7',
    'Item 8',
    'Item 9',
    'Item 10',
    'Item 11',
    'Item 12',
    'Item 13',
    'Item 14',
    'Item 15',
    'Item 16',
    'Item 17',
    'Item 18',
    'Item 19',
    'Item 20',
    'Item 21',
    'Item 22'
];

👉Now we get the List and Pagination in our JavaScript code.

custom pagination javscript fetch the items and page
let list_elemnts = document.querySelector('#list');
let pagination_elemnts = document.querySelector('#pagination');

👉 Now we defined current page and row. A row means how many to show rows in our page.

rocoderes-custom pagination in javsacript -rows
let current_page = 1;
let row = 5;

Display Items

👉 Now we will see how we can display the items. Now we create the Function DisplaysItems. In DisplaysItems we will pass 4 parameters which is current_page, row, items and wrapper. First We have to display the array Items. Here we have to show only 5 items in every page. That’s why we have taken two variables here, start and end. Where the start value will be 0 and end value will be 5.

👉 Now we will store the value of 0 to 5 of the items in another array using slice method. Now we have an array PaginationItems which have total 5 values. Now we will use map method on PaginationItems to display all the items one by one. Here you can also use for loop.

custom pagination in javascript- display items
custom pagination in javascript- display items
function DisplaysItems(current_page, row, items, wrapper) {
    wrapper.innerHTML = '';
    current_page--;


    let start = current_page * row;
    let end = start + row;
    
    let PaginationItems = items.slice(start, end);

    PaginationItems.map((item) => {
        let items_elemnts = document.createElement('div');
        items_elemnts.classList.add('item');
        items_elemnts.innerText = item;
        wrapper.appendChild(items_elemnts);
    });
}

create Pagination Button

👉Let’s create pagination button. Here we create a function which is PaginationButton. In PaginationButton we will pass 2 parameters which is items and page. In this function we create a button element and assign the page variable in text. Here we will give a condition If the value of current_page end page is value is equal then we add a class on button. Now we will use click event on button.In click event we call DisplaysItems function.

function PaginationButton(items, page) {
     let button = document.createElement('button');
     button.innerText = page;
     if (current_page === page) {
         button.classList.add('active');
     }
     button.addEventListener('click', function() {
         current_page = page;
         DisplaysItems(current_page, row, items, list_elemnts);
         let current_btn = document.querySelector('.pagenumbers button.active');
         current_btn.classList.remove('active');
         button.classList.add('active');
     });
     return button;
 }

Set The Pagination

👉 Now we create setUpPagination function. In this function we will pass 3 parameters which is items, row and pagination_elemnts. Now we create a variable which is pageCount. The value of pageCount is items.length/rows. After we will create a for loop. inside loop we call the PaginationButton function.

function setUpPagination(items, row, pagination_elemnts) {
     pagination_elemnts.innerHTML = '';
     let PageCount = Math.ceil(items.length / row);
     for (let i = 1; i < PageCount + 1; i++) {
         let btn = PaginationButton(items, i);
         pagination_elemnts.appendChild(btn);
     }
 }

full source code: Custom Pagination in javascript

👉 If you want to clear the basic concepts of JavaScript you must read this:
➜ 15 JavaScript Basic Concepts You Should Know

Related

Subscribe to Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Share this Article
Facebook Twitter Email Print
What do you think?
Love0
Sad0
Happy0
Sleepy0
Angry0
Dead0
Wink0
Posted by Admin
Follow:
Rocoderes is a blog you can learn HTML, CSS, JavaScript, React Js and Python along with creative coding stuff and free source code files.
Previous Article Split method in python Python String split() Method
Next Article 15 JavaScript Basic Concepts You Should Know 15 JavaScript Basic Concepts You Should Know
22 Comments 22 Comments
  • Nikhil Sharma says:
    June 26, 2021 at 11:53 am

    I read your post. It is very informative and helpful to me. I admire the message valuable information you provided in your article.
    power bi certification

    Reply
    • patelrohan7500 says:
      June 26, 2021 at 12:01 pm

      Thank you bro ❤

      Reply
  • Prajapati Rahul says:
    July 27, 2021 at 3:26 pm

    Nice and lovely blog, i loved it so informative

    Keep it up

    Reply
  • Prajapati Rahul says:
    July 27, 2021 at 3:28 pm

    I want to give you suggestion, that you should add two buttons for previous and next blog . It will help users to find your other awesome blogs..

    Reply
    • patelrohan7500 says:
      July 28, 2021 at 4:07 am

      yes sure

      Reply
  • JohnnyPibia says:
    August 12, 2021 at 5:43 pm

    It is remarkable, very amusing opinion

    Reply
  • JohnnyPibia says:
    August 15, 2021 at 1:34 pm

    Yes, really. All above told the truth. Let’s discuss this question. Here or in PM.

    Reply
  • Danielmub says:
    August 16, 2021 at 7:52 pm

    I can suggest to visit to you a site, with a large quantity of articles on a theme interesting you.

    Reply
  • Jamaal says:
    August 17, 2021 at 2:44 am

    Hello there, You’ve done a fantastic job. I will certainly digg it and personally recommend to
    my friends. I am sure they’ll be benefited from this web site.

    Reply
  • Pingback: Javascript convert date to long date format - rocoderes
  • Rosalinda says:
    November 17, 2021 at 2:37 am

    magnificent publish, very informative. I’m wondering why
    the opposite specialists of this sector don’t
    understand this. You should proceed your writing.
    I am sure, you have a great readers’ base already!

    Reply
  • Leandra says:
    November 18, 2021 at 11:02 pm

    I think this is among the most vital info for me.
    And i’m glad reading your article. But want to remark on some general things, The website style is wonderful, the articles is really nice : D.
    Good job, cheers

    Reply
  • Naomi says:
    November 22, 2021 at 6:41 pm

    Great weblog right here! Also your site lots up fast!
    What web host are you the use of? Can I am getting your associate hyperlink on your host?
    I desire my web site loaded up as fast as yours lol

    Reply
  • tinyurl.com says:
    March 23, 2022 at 3:04 am

    bookmarked!!, I really like your web site!

    Reply
  • tinyurl.com says:
    March 23, 2022 at 6:14 pm

    There’s definately a great deal to know about this subject.
    I really like all the points you made.

    Reply
  • bit.ly says:
    March 24, 2022 at 6:03 am

    Thanks for finally writing about > Custom Pagination in JavaScript(beginners) – rocoderes < Loved it!

    Reply
    • Admin says:
      March 26, 2022 at 6:19 am

      Thanks men

      Reply
  • tinyurl.com says:
    March 24, 2022 at 11:07 am

    It’s awesome to go to see this website and reading the views of all mates concerning this piece of writing, while I am
    also keen of getting familiarity.

    Reply
  • Rufus Brumit says:
    May 2, 2022 at 5:11 am

    Interesting blog! Is your theme custom made or did you download it from somewhere? A theme like yours with a few simple adjustements would really make my blog stand out. Please let me know where you got your design. Thanks

    Reply
  • Serena Nasta says:
    May 22, 2022 at 8:06 pm

    I¦ve been exploring for a little bit for any high-quality articles or weblog posts in this sort of space . Exploring in Yahoo I eventually stumbled upon this site. Reading this info So i am satisfied to express that I’ve an incredibly excellent uncanny feeling I discovered just what I needed. I so much unquestionably will make sure to do not overlook this site and provides it a look on a constant basis.

    Reply
  • houston junk car buyer says:
    May 25, 2022 at 9:11 am

    Highly energetic post, I enjoyed that bit.
    Will there be a part 2?

    Reply
    • Admin says:
      May 28, 2022 at 6:01 am

      No Bro

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

- Advertisement -

You Might Also Like

How to set the dropdown value by clicking on a table row

Javascript – How to set the dropdown value by clicking on a table row

February 11, 2024
Attempting to increase the counter, when the object's tag exist

Javascript – Attempting to increase the counter, when the object’s tag exist

February 11, 2024
Cycle2 JS center active slide

Javascript – Cycle2 JS center active slide

February 10, 2024
Can import all THREE.js post processing modules as ES6 modules except OutputPass

Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass

February 10, 2024
rocoderesrocoderes
Follow US

Copyright © 2022 All Right Reserved By Rocoderes

  • Home
  • About us
  • Contact us
  • Disclaimer
Join Us!

Subscribe to our newsletter and never miss our latest news, podcasts etc.

Zero spam, Unsubscribe at any time.
Welcome Back!

Sign in to your account

Lost your password?