Display Page Number in HTML W3Schools Tutorial Example

When it comes to navigating through web pages, users often find it helpful to know where they are in the sequence of pages. This is where display page numbers in HTML come into play. By implementing this feature, you can enhance user experience and accessibility. Let's explore how you can achieve this with the help of HTML, CSS, and JavaScript.

🌐 How Browsers Display Web Pages
🌐 How Browsers Display Web Pages

Before diving into the code, let's understand why displaying page numbers is crucial. It assists users in understanding how many pages they need to go through to find the information they need. It also helps them bookmark a specific page quickly. Now, let's jump into the steps to display page numbers in HTML.

an image of a phone screen with the text,'how many numbers are there? '
an image of a phone screen with the text,'how many numbers are there? '

Implementing Simple Pagination

We'll start with a simple example where we have an array of data, and we want to display 10 items per page. We'll use plain JavaScript and HTML for this.

CSS Paginations
CSS Paginations

First, let's create an array of data. We'll use the W3Schools' data for bicycle marks as an example:

Creating Data Array

Inline vs Block Elements | HTML Display Explained
Inline vs Block Elements | HTML Display Explained

In your JavaScript file, define your data array as follows:

let data = [['Bike', 1000], ['Car', 2000], ['Bicycle', 300], ['Railway', 500], ...];

Initializing Variables & DOM Elements

🧾 How to Create Tables in HTML
🧾 How to Create Tables in HTML

Next, initialize variables for current page and total pages. Also, select the elements where you'll display data and page numbers:

let currentPage = 1; let totalPages = Math.ceil(data.length / 10); let itemsPerPage = 10; let dataTable = document.getElementById('data-table'); let pagination = document.getElementById('pagination');

Pagination using HTML, CSS, and JavaScript

Favicon in HTML: Add Bookmark & App Icons 🎨🌍
Favicon in HTML: Add Bookmark & App Icons 🎨🌍

Now, let's create a function to generate page numbers and display them. We'll useful flags to keep track of whether we're on the first or last page.

Inside your JavaScript file:

HTML vs CSS vs JavaScript: What Each One Actually Does (Beginner's Visual Guide)
HTML vs CSS vs JavaScript: What Each One Actually Does (Beginner's Visual Guide)
How to use Data tables in HTML code ? - EcomSpark
How to use Data tables in HTML code ? - EcomSpark
CSS Display | CSS Tutorial
CSS Display | CSS Tutorial
Input Type In HTML
Input Type In HTML
a colorful display with lots of numbers on it
a colorful display with lots of numbers on it
Vocabulary, Numeracy Classroom Display, Continuous Provision
Vocabulary, Numeracy Classroom Display, Continuous Provision
CSS Units of Measure: Going Beyond the Pixel
CSS Units of Measure: Going Beyond the Pixel
زينة مختبر الحاسوب.. وسوم لغة البرمجة التشعبية html
زينة مختبر الحاسوب.. وسوم لغة البرمجة التشعبية html
an info sheet describing the different types of web pages and how they are used to create them
an info sheet describing the different types of web pages and how they are used to create them
Coding a Stylish Blog Design Layout in HTML & CSS
Coding a Stylish Blog Design Layout in HTML & CSS
Number Line Banner Ideas To Inspire Your Classroom Display
Number Line Banner Ideas To Inspire Your Classroom Display
an image of a web page with different types of text and symbols on it, including the
an image of a web page with different types of text and symbols on it, including the
Learn HTML5 and CSS3
Learn HTML5 and CSS3
CSS Display Values Cheatsheets
CSS Display Values Cheatsheets
HTML uses tags to control the structure of a web page
HTML uses tags to control the structure of a web page
Login and Registration Form in HTML & CSS
Login and Registration Form in HTML & CSS
Welcome to SiteRaw
Welcome to SiteRaw
Visual Display Numbers
Visual Display Numbers
Ways to Show Numbers Anchor Chart
Ways to Show Numbers Anchor Chart
programming a simple calculator
programming a simple calculator

Generating Page Numbers

function generatePagination() { pagination.innerHTML = ''; let li = document.createElement('li'); li.innerHTML = '< previous'; if(currentPage != 1){ li.setAttribute('onclick', 'currentPage -= 1; renderData(); generatePagination();'); } else { li.classList.add('disabled'); } pagination.appendChild(li); for(let i = 1; i <= totalPages; i++){ li = document.createElement('li'); li.innerHTML = i; if(i == currentPage){ li.classList.add('active'); } else { li.setAttribute('onclick', 'currentPage = ' + i + '; renderData(); generatePagination();'); } pagination.appendChild(li); } li = document.createElement('li'); li.innerHTML = 'next >'; if(currentPage != totalPages){ li.setAttribute('onclick', 'currentPage += 1; renderData(); generatePagination();'); } else { li.classList.add('disabled'); } pagination.appendChild(li); }

Displaying Data on Each Page

Finally, create a function to render data based on the current page:

function renderData() { dataTable.innerHTML = ''; let start = (currentPage * itemsPerPage) - itemsPerPage; let end = start + itemsPerPage; let tableData = data.slice(start, end); for(let i = 0; i < tableData.length; i++){ let row = dataTable.insertRow(); for(let j = 0; j < tableData[i].length; j++){ row.insertCell().innerHTML = tableData[i][j]; } } }

Now, call these functions when your page loads:

generatePagination(); renderData();

With this implementation, users can now navigate through your pages easily. The display of page numbers not only improves user experience but also aids in accessibility, making your web pages more user-friendly.

Related Articles

W3schools Element Reference W3schools Html Css Flexbox W3schools Href Button W3schools Html Datetime Difference Between Div And Paragraph In Html W3 Schools Html Class Div Tag In Html Example W3schools W3schools Html Download Link W3schools Html Email Link W3s Html Button