How to create Multiplication Table using HTML, CSS, and JavaScript

Discover how to construct a dynamic multiplication table builder utilizing HTML, CSS, and JavaScript. This step-by-step guide caters to individuals at all skill levels.

In our current digital era, web development stands out as a vibrant and approachable domain that enables people to build interactive tools for various needs. One such tool is the Multiplication Table Builder. This clever invention allows users to create multiplication tables for any specified number, enhancing the convenience and efficiency of a basic mathematical task.

In this detailed guide, we will simplify the steps involved in creating the Multiplication Table Builder. Whether you’re a beginner just taking your journey into web development or an experienced coder aiming to expand your expertise, this tutorial provides valuable insights. By the end of this article, you will not only possess a working multiplication table but also gain a richer understanding of how these technologies work together seamlessly  to create an outstanding user experience.

To begin, we’ll initially need to establish a fundamental HTML file. Within this file, we will incorporate the primary framework for our multiplication table builder.

After creating your HTML file, paste the provided code and save the document with a .html extension for proper viewing in a web browser.

The code includes declaration <!DOCTYPE html> for HTML5, the opening <html> tag with a language attribute lang=”en” as English, and a <head> section for metadata and resources. It sets character encoding to UTF-8, specifies view port settings, includes compatibility mode for Internet Explorer, links to Bootstrap CSS file, and sets the page title for the browser’s title bar.

The <body> section of the webpage includes visible content and features a Bootstrap  container for alignment and padding. It contains an <h3> heading titled “Multiplication Table Builder,” a centered paragraph describing the tool, and a form with input fields for users to enter numbers and a submit button for generating the table. Additionally, there is an empty <div> for displaying the generated table and a link to an external JavaScript file for interactivity.

This outlines the basic structure of the Multiplication Table Builder in HTML, leading to the next step of styling it with CSS.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;500;600;700&display=swap">
    <link rel="stylesheet" href="styles.css">
    <title>Multiplication Table | Coding Jasim</title>
</head>
<body>
    <div class="container">
        <section>
            <h3 id="title" class="centerText">Multiplication Table Builder</h3>
            <p style="text-align:center;font-size:12px;margin-bottom: 50px;margin-top: -20px;">(get the multiplication table for any digit just by entering the number along with the desired range)</p>
            <form onsubmit="return false">
                <div class="form-group">
                    <div class="col-md-12">
                        <input style="font-size:12px;" type="number" id="text-Number" class="form-control" min="0"
                            placeholder="Enter the number which table you want... ">
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-md-12">
                        <input style="font-size:12px;" type="number" id="text-Interval" class="form-control" min="0" max="100"
                            placeholder="The limit or the highest number you desire for your table should be..">
                        <span class="small" >The highest value in the range is 100, since exceeding this value may cause your browser to crash.</span>
        
                    </div>
                </div>
                <div id="generate" class="form-group">
                    <div class="col-md-12">
                        <input type="submit" id="button-Generate" value="Retrieve Table">
                    </div>
                </div>
                <div id="ans"></div>
           
            </form>
            </section>
    </div>
    <script src="script.js"></script>
</body>
</html>

 Once the essential  HTML framework of the Multiplication Table Generator is established, the subsequent step is to apply styling to the Builder using CSS. In this file, we will implement some basic CSS rules to design our builder.

The design of the webpage uses the ‘Poppins’ font for the body and includes a container element with a maximum width of 500 pixels, which is centered on the page. Inside the container, a section with a dark green background and larger text features and rounded corners. A button ID “btnGenerate” takes up 50% of its parent width has pink background with white uppercase text, and rounded borders that change color on hover.

Additionally, an element with the ID “generate” has padding applied for spacing, enhancing the overall presentation of the Multiplication Table Builder. 

body {
    font-family: 'Poppins','Gill Sans', 'Gill Sans MT', Calibri, 'Trebuchet MS', sans-serif;
    color: rgb(248, 248, 248);
    background-color: #0b7cfd;
}
.container {
    padding-top: 10px;
    max-width: 500px;
    margin: 20px auto;
}

section {
  background: #0d5254;
    font-size: 1.2em;
    text-align: center;
    border-radius: 10px;
    margin-top: 50px;
}

#title {
    padding-top: 10px;
    margin-bottom: 20px;
    text-transform: uppercase;
}

#ans {
    margin-top: 2%;
    background-color: rgb(244, 10, 248);
    font-weight: bold;
}

#button-Generate {
    width: 50%;
    height: 40px;
    background-color: rgb(251, 7, 125);
    border: none;
    border-radius: 7px;
    color: #fff;
    text-transform: uppercase;
  outline: none;
}
#button-Generate:hover{
    background-color: rgb(176, 3, 87);
}

.container > section > form > #generate { padding: 20px; }

Lastly, we need to formulate a function using JavaScript. Let’s go through the code step by step:

Four variables are defined to allocate reference different HTML elements: ‘text-Number’ for a number input, ‘text-Interval’ for an interval input, ‘button-Generate’ for a button to trigger calculations and ‘ans’ for displaying results. An event listener is added to the document to call the ‘onFocus’ function when the page loads, ensuring that the focus is set on the ‘text-Number’ input field.

Two event listeners are attached to the text-Interval and text-Number input fields for the ‘keydown’ event, which triggers the onKeyDown function to prevent typing the letter ‘e’. The onClick function initializes a count variable to manage multiplication iterations, validates the text-Number input; and perform calculations if valid, then displaying results in the ‘ans’ element.

The validateNumber function checks if the number is empty and clears the ans element if necessary, while the validateInterval function verifies if the interval is greater than 100 and clears the ans element if it is. Both validate functions return a value indicating the validity of their respective inputs. Additionally, the onFocus function sets the focus on the text-Number input field when the page loads.

var txtNumber = document.getElementById('text-Number');
var txtInterval = document.getElementById('text-Interval');
var btnGenerate = document.getElementById('button-Generate');
var ans = document.getElementById('ans');

document.addEventListener('DOMContentLoaded', onFocus);
btnGenerate.addEventListener('click', onClick);
txtInterval.addEventListener('keydown', onKeyDown);
txtNumber.addEventListener('keydown', onKeyDown);
 
function onClick() {
    let count = 0;
    if (!validateNumber(txtNumber.value)) {
        while (count <= txtInterval.value) {
            ans.innerHTML += parseFloat(txtNumber.value) + ' X ' + count + ' = ' + (parseFloat(txtNumber.value) * count) + '<br>';
            count++;
        }
        validateInterval(txtInterval.value);
    }
    return count;
}

function validateNumber(number) {
    let ret = false;
    if (number == '') ret = true;
    else if (number != '' && ans != '') ans.innerHTML = '';
    return ret;
}

function validateInterval(interval) {
    let ret = false;
    if (interval > 100) {
        ans.innerHTML = '';
        ret = true;
    }
    return ret;
}

function onKeyDown(e) {
    if (e.key === 'e') e.preventDefault();
}

function onFocus() {
    txtNumber.focus();
}

Conclusion & Final Thoughts

To sum up, the journey to building a Multiplication Table Builder with HTML, CSS, and JavaScript has been a fulfilling dive into the opportunities within web development. Throughout this project, we have utilized the strengths of each technology to develop a functional and user-friendly application.

From establishing the fundamental HTML layout to enhancing your builder with CSS styling, and ultimately integrating dynamic functionality using JavaScript, we have observed how these three elements work together seamlessly to create an engaging user experience. This tutorial has not only provided you with the technical know-how to construct a multiplication table builder but has also offered insight into the creative and analytical dimensions of web development.

The multiplication table builder serves as a simple illustration of how web technologies can simplify daily tasks and improve information accessibility. As you progress on your coding path, think about how you might use similar concepts to tackle other challenges with different innovations.

If you run into any issues while developing your builder, you can freely download the source code files for this builder project by clicking the Download button.

Download Button with Timer | CodingJasim
Scroll to Top