How can HTML tables be utilized to display a multiplication table?.querySelector() in JavaScript helps achieve this

I have successfully written some code that is functioning perfectly. My goal is to display the multiplication table using HTML tables and apply Bootstrap to style it with borders on each line represented as a table row 'tr'. The table should be styled using the Bootstrap table-bordered class.

<!DOCTYPE html>
<html>
<head>
  <title>Multiplication Table</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
</head>
<body>

<h2>MultiplicationTable</h2

<form action>
Table Number:<br>
<input type="text" id="TN" name="TableNumber">
<br>
Initial Number:<br>
<input type="text" id="IN" name="InitialNumber">
<br>
Ending Number:<br>
<input type="text" id="EN" name="EndingNumber">
</form>

<br><br>    
   

<button onclick="myFunction()">Print Table</button>

 

<p id="MT"></p>


<script>
function myFunction()
{
var text = "";
var Number = document.getElementById("TN").value;
var T;
var I = document.getElementById("IN").value;
var E = document.getElementById("EN").value;

for (T = I; T <= E; T++) {


     text += Number + "*" + T + "=" + Number*T + "<br>"; 

}


document.getElementById("MT").innerHTML = text;
}

    
</script>

Answer №1

Create a basic multiplication table using JavaScript.

Insert code below:

<!DOCTYPE html>
<html>

<head>
    <title>Multiplication Table</title>
</head>

<body>
    <script type="text/javascript">
    var row, col;
    var num = 1;

    for (row = 1; row <= 10; row++) {
        for (col = 1; col <= 10; col++) {
            document.write(row  *  col);
        }
        document.write("<br/>");
    }
    </script>
</body>

</html>

Answer №2

function generateMultiplicationTable()
{

var number = document.getElementById("TN").value;
var tableNumber;
var initialNumber = document.getElementById("IN").value;
var endingNumber = document.getElementById("EN").value;
var tableData="";
for (tableNumber = initialNumber; tableNumber <= endingNumber; tableNumber++) {
tableData+="<tr><td>"+number+"</td><td>*</td><td>" + tableNumber + "</td><td>=</td><td>" + number*tableNumber +"</td></tr>"; 
}

$("#displayTables").append(tableData);

}
<!DOCTYPE html>
<html>
<head>
  <title>Multiplication Table</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
</head>
<body>

<h2>MultiplicationTable</h2

<form action>
Table Number:<br>
<input type="text" id="TN" name="TableNumber">
<br>
Initial Number:<br>
<input type="text" id="IN" name="InitialNumber">
<br>
Ending Number:<br>
<input type="text" id="EN" name="EndingNumber">
</form>

<br><br>    


<button onclick="generateMultiplicationTable()">Print Table</button>
<table class="table table-bordered"  id="displayTables">

</table>
 

Answer №3

<!DOCTYPE html>
<html>
<head>
  <title>Multiplication Table</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
</head>
<body>

<h2>MultiplicationTable</h2

<form action>
Table Number:<br>
<input type="text" id="TN" name="TableNumber">
<br>
Initial Number:<br>
<input type="text" id="IN" name="InitialNumber">
<br>
Ending Number:<br>
<input type="text" id="EN" name="EndingNumber">
</form>

<br><br>    
   

<button onclick="myFunction()">Print Table</button>

 

<p id="MT"></p>


<script>
function myFunction()
{
var text = "";
var N = document.getElementById("TN").value;
var M;
var I = document.getElementById("IN").value;
var E = document.getElementById("EN").value;

for (M = I; M <= E; M++) {


     text += N + "*" + M + "=" + N*M + "<br>"; 

}


document.getElementById("MT").innerHTML = text;
}

    
</script>

8

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Issues have been observed with the keyframe animation involving a JavaScript timer, specifically in the rotation of the outer circle around the inner circle

I seem to be encountering an issue with an animation. My goal is to have three arrows in a circle that rotate around an inner circle and pause every 3 seconds for another 3 seconds before continuing to rotate. However, instead of rotating smoothly, the ani ...

Not Getting Two-Way Data Binding to Work Properly in AngularJS Controller

Hey there S.O. Gurus! I've hit a roadblock with the two-way data binding in one of my controllers. While it's working smoothly in other controllers, this particular one is causing me some trouble. Any assistance would be greatly appreciated! Con ...

Implementing velocity.js for hover effects - a comprehensive guide

I'm seeking guidance on incorporating velocity.js for hover effects on elements. Currently, I utilize CSS for zooming in and out and animating elements upon user hover, while using velocity.js for initial page load animations. My query is: how shoul ...

How can I make the hover effect only apply to the text and not the entire box?

I'm wondering how I can make the :hover property only affect individual letters in my navigation bar, instead of hovering over the entire box. * { margin: 0px; padding: 0px; } .navigation-bar { height: 3.2em; background: gray; text-align: ...

Is there a way to adjust the positioning of an image within a <div> element?

I'm attempting to vertically align an image within a horizontal menu bar. My goal is to adjust the padding/margin of the image using inline CSS. However, I've noticed that when I add margin-top or padding-top, it affects the positioning of all m ...

Differences in vertical font positioning between Mac and Windows systems

One element on my page is a quantity balloon for a basket, but I'm struggling to get it looking right. The font and vertical position don't seem to be cooperating for such a simple element: https://i.sstatic.net/IfSi6.png Despite trying solutio ...

The Viadeo Social Toolbox seems to be encountering technical difficulties at the moment

I attempted to utilize a Viadeo Social Toolbox, specifically the Viadeo Share Button, but it seems to be malfunctioning in certain browsers? I came across some outdated viadeo share URLs like this: http://www.viadeo.com/shareit/share/?url=${url}&title ...

Oops! Looks like there was an error: weight is not defined when trying to calculate BMI with the HTMLButtonElement

I'm currently working on a project to create a BMI calculator following specific instructions. I've managed to follow all the guidelines except one regarding the letsCalculateBMI function. The instruction states: letsCalculateBMI should extrac ...

One effective way to redirect after a PUT request and display a one-time message

Here's what I'm aiming for in terms of desired behaviour: A user navigates to a password change web page. The user completes the form and it is sent via PUT request to our REST server. Upon successful completion, the user is redirected to their ...

Modify the class's height by utilizing props

Using Vue 3 and Bootstrap 5, I have a props named number which should receive integers from 1 to 10. My goal is to incorporate the number in my CSS scrollbar class, but so far it's not working as expected. There are no error messages, it just doesn&a ...

Renaming errors within a project with a complex nested structure using npm

I am encountering an issue in my NodeJS project which consists of nested subprojects with their own package.json files. Whenever I make changes to dependencies in the subprojects, I encounter errors similar to the one below: npm ERR! code ENOENT npm ERR! ...

switch out javaScript for selenium

I'm attempting to replace the JavaScript functionality of a web page using Selenium (in Java with Firefox Geckodriver, if that makes a difference). Consider this webpage: <HTML><HEAD></HEAD> <BODY> <DIV id="time">Ti ...

Activate simultaneous HTML5 videos on iOS devices with a simple click

I am currently in the process of developing a webpage that will play various videos when specific elements are clicked. Although the functionality works perfectly on desktop computers, it encounters an issue on iOS devices. The problem arises when the elem ...

Change Scenery by Using Arrow Keys

I am trying to increase the speed of my background when the up and arrow keys are pressed. Despite my attempts, it is not working as expected. The variable this.speed controls the speed of the background. To achieve this, I implemented an if statement t ...

Hover or click on HTML input type using CSS in your webpage

I am currently working on adding hover effects to my elements. I have successfully added a hover effect to the outer list item, but I am wondering how I can achieve the same effect on the <input> element instead of the <li>. ul.slides li:hov ...

Preventing background scrolling while using a fixed overlay in jQuery/CSS

I'm working on an overlay box that is fixed and centered on the screen. The page it's being used on is pretty lengthy and has a vertical scrollbar. My goal is to prevent scrolling of the main page when the overlay is displayed. However, I can&ap ...

Update and verify a collection of objects in real-time

I have a callback function that retrieves a data object from the DOM. Each time an item is selected, the function returns an object like this: $scope.fClick = function( data ) { $scope.x = data; } When ...

Tips on using javascript to reset an element's inline CSS to its initial default styling?

I have a basic function that alters the default text styling when a checkbox is checked, and restores it to its original state when the checkbox is unchecked. Despite my efforts, the terms.style = ""; line does not reset the style as expected. I am perple ...

Steps for ensuring the search icon remains visible in the column of ag-grid

I am looking to find a way to keep the filter icon constantly visible in the Ag-grid interface. Currently, the filter icons only show up when I hover over the column headings. Here is an example of the column definition that I have been using: this.Colum ...

Filtering out elements from an array using jQuery

I am looking for an efficient way to maintain an array of elements by adding and removing arrays of elements dynamically. var myElements = $('.initial'); Instead of creating a new array, jquery's merge() allows me to add items to the exist ...