Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="widget-wrap">
  <div id="book"></div>
  <h1>SIMPLE HTML JS PAGINATION</h1>
  
  <strong class="spaced">PAGINATION WITH URL STRING</strong>
  <div id="demoA"></div>
  
  <strong class="spaced">PAGINATION WITH JS FUNCTION</strong>
  <div id="demoB"></div>
  
  <!-- (X) VISIT CODE-BOXX -->
  <div id="code-boxx">
    Visit
    <a href="https://code-boxx.com/simple-pagination-javascript-html/"
       target="_blank">
      Code Boxx
    </a> for more details.
  </div>
</div>
              
            
!

CSS

              
                /* (A) PAGINATION */
paginate { display: flex; }
.paginate a {
  color: #000;
  background: #f2f2f2;
  padding: 8px 10px;
  margin: 2px;
  text-decoration: none;
  cursor: pointer;
}
.paginate a.current {
  color: #fff;
  background: #4486ff;
}
.paginate a:hover {
  color: #000;
  background: #ffe9e9;
}

/* (X) THE REST DOES NOT MATTER - COSMETICS */
/* PAGE & BODY */
strong.spaced {
  display: block;
  margin: 30px 0 20px 0;
}
* {
  font-family: arial, sans-serif;
  box-sizing: border-box;
}
body {
  display: flex;
  align-items: center; justify-content: center;
  min-height: 100vh;
  background-image: url(https://images.unsplash.com/photo-1554034483-04fda0d3507b?crop=entropy&cs=srgb&fm=jpg&ixid=MnwxNDU4OXwwfDF8cmFuZG9tfHx8fHx8fHx8MTY0NzAwOTc2NA&ixlib=rb-1.2.1&q=85);
  background-repeat: no-repeat;
  background-position: center;
  background-size: cover;
  text-align: center;
}

/* WIDGET */
.widget-wrap {
  width: 400px;
  padding: 30px;
  border-radius: 20px;
  background: rgba(255, 255, 255, 0.4);
}

/* SVG */
#book {
  width: 100%; height:120px;
  background-image: url('data:image/svg+xml;utf8,<svg viewBox="0 0 576 512" width="100" xmlns="http://www.w3.org/2000/svg"><path d="M542.22 32.05c-54.8 3.11-163.72 14.43-230.96 55.59-4.64 2.84-7.27 7.89-7.27 13.17v363.87c0 11.55 12.63 18.85 23.28 13.49 69.18-34.82 169.23-44.32 218.7-46.92 16.89-.89 30.02-14.43 30.02-30.66V62.75c.01-17.71-15.35-31.74-33.77-30.7zM264.73 87.64C197.5 46.48 88.58 35.17 33.78 32.05 15.36 31.01 0 45.04 0 62.75V400.6c0 16.24 13.13 29.78 30.02 30.66 49.49 2.6 149.59 12.11 218.77 46.95 10.62 5.35 23.21-1.94 23.21-13.46V100.63c0-5.29-2.62-10.14-7.27-12.99z" /></svg>');
  background-repeat: no-repeat;
  background-position: center;
}

/* FOOTER */
#code-boxx {
  font-weight: 600;
  margin-top: 30px;
}
#code-boxx a {
  display: inline-block;
  padding: 5px;
  text-decoration: none;
  background: #b90a0a;
  color: #fff;
}
              
            
!

JS

              
                function paginator (instance) {
// target : html element to generate pagination.
// total : total number of pages.
// click : url string or function to call on click.
// current : (optional) current page, default 1.
// adj : (optional) num of adjacent pages beside "current page", default 2.
 
  // (A) INIT & SET DEFAULTS
  if (instance.current === undefined) {
    let param = new URLSearchParams(window.location.search);
    instance.current = param.has("pg") ? Number.parseInt(param.get("pg")) : 1;
  }
  if (instance.adj === undefined) { instance.adj = 2; }
  if (instance.adj <= 0) { instance.adj = 1; }
  if (instance.current <= 0) { instance.current = 1; }
  if (instance.current > instance.total) { instance.current = instance.total; }

  // (B) URL STRING ONLY - DEAL WITH QUERY STRING & APPEND PG=N
  const jsmode = typeof instance.click == "function";
  if (jsmode == false) {
    if (instance.click.indexOf("?") == -1) { instance.click += "?pg="; }
    else { instance.click += "&pg="; }
  }
 
  // (C) HTML PAGINATION WRAPPER
  instance.target.innerHTML = "";
  instance.target.classList.add("paginate");
 
  // (D) DRAW PAGINATION SQUARES
  // (D1) HELPER FUNCTION TO DRAW PAGINATION SQUARE
  const square = (txt, pg, css) => {
    let el = document.createElement("a");
    el.innerHTML = txt;
    if (css) { el.className = css; }
    if (jsmode) { el.onclick = () => { instance.click(pg); }; }
    else { el.href = instance.click + pg; }
    instance.target.appendChild(el);
  };

  // (D2) BACK TO FIRST PAGE (DRAW ONLY IF SUFFICIENT SQUARES)
  if (instance.current - instance.adj > 1) { square("&#10218;", 1, "first"); }

  // (D3) ADJACENT SQUARES BEFORE CURRENT PAGE
  let temp;
  if (instance.current > 1) {
    temp = instance.current - instance.adj;
    if (temp<=0) { temp = 1; }
    for (let i=temp; i<instance.current; i++) { square(i, i); }
  }
 
  // (D4) CURRENT PAGE
  square(instance.current, instance.current, "current");

   // (D5) ADJACENT SQUARES AFTER CURRENT PAGE
  if (instance.current < instance.total) {
    temp = instance.current + instance.adj;
    if (temp > instance.total) { temp = instance.total; }
    for (let i=instance.current+1; i<=temp; i++) { square(i, i); }
  }
 
  // (D6) SKIP TO LAST PAGE (DRAW ONLY IF SUFFICIENT SQUARES)
  if (instance.current <= instance.total - instance.adj - 1) {
    square("&#10219;", instance.total, "last");
  }
}

// (E) PAGINATION HANDLER
function load (pg) {
  /* (E1) AJAX FETCH PAGE
  fetch ("http://site.com/?pg="+pg)
  .then(res => res.text())
  .then((txt) => {
    document.getElementById("WRAP").innerHTML = txt;
  }); */
 
  // (E2) REDRAW PAGINATION
  paginator({
    target : document.getElementById("demoB"),
    total : 10,
    current : pg,
    click : load
  });
}

// (F) ATTACH PAGINATION
window.addEventListener("load", () => {
  // (F1) PAGINATION WITH URL STRING
  paginator({
    target : document.getElementById("demoA"),
    total : 10,
    click : "page.html"
  });
  
  // (F2) PAGINATION WITH JS FUNCTION
  load(1);
});
              
            
!
999px

Console