JavaScript - Auto Calculate Total

In this tutorial we will create a Auto Calculate Total using JavaScript. This code will automatically the given number when the user enter it in the form field. The code use onkeyup() to call a method that can will calculate immediately the inputted number by calling autoCalculate() function. You can apply this script to your code to make your transaction faster, it is a user-friendly program feel free to modify it. We will use JavaScript to add some new feature to the website interface by actually written into an HTML page. It is what gives your page a different interactive elements and animation that engage a user.

Getting Started:

This is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

The Main Interface

This code contains the interface of the application. To create this just write these block of code inside the text editor and save this as index.html.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.         <head>
  4.                 <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5.                 <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6.         </head>
  7.         <nav class="navbar navbar-default">
  8.                 <div class="container-fluid">
  9.                         <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  10.                 </div>
  11.         </nav>
  12.         <div class="col-md-3"></div>
  13.         <div class="col-md-6 well">
  14.                 <h3 class="text-primary">JavaScript - Auto Calculate Total</h3>
  15.                 <hr style="border-top:1px dotted;"/>
  16.                 <div class="col-md-4">
  17.                         <div class="form-group">
  18.                                 <input type="number"class="form-control" id="number1" onkeyup="autoCalculate()" placeholder="Enter a number"/>
  19.                         </div>
  20.                         <div class="form-group">
  21.                                 <select id="symbol" class="form-control" onchange="autoCalculate()">
  22.                                         <option value="add">+</option>
  23.                                         <option value="sub">-</option>
  24.                                         <option value="mult">x</option>
  25.                                         <option value="div">/</option>
  26.                                 </select>
  27.                         </div>
  28.                         <div class="form-group">
  29.                                 <input type="number"class="form-control" id="number2" onkeyup="autoCalculate()" placeholder="Enter a number"/>
  30.                         </div>
  31.                 </div>
  32.                 <div class="col-md-8">
  33.                         <div id="result"></div>
  34.                 </div>
  35.         </div>
  36. <script src="js/script.js"></script>   
  37. </body>
  38. </html>

Creating the Script

This code contains the script of the application. This code will dynamically calculate a number when the value is entered. To do this just copy and write these block of codes as shown below inside the text editor and save it as script.js inside the js directory
  1. function autoCalculate(){
  2.         var number1 = document.getElementById('number1').value;
  3.         var number2 = document.getElementById('number2').value;
  4.         var symbol = document.getElementById('symbol').value
  5.         if(number1 != "" && number2 != ""){
  6.                 switch(symbol){
  7.                         case "add":
  8.                                 var digit1 = parseFloat(number1);
  9.                                 var digit2 = parseFloat(number2);
  10.                                 var total = digit1 + digit2;
  11.                                 document.getElementById('result').innerHTML = "<h2>The answer is: "+total+"</h2>";
  12.                         break;
  13.                         case "sub":
  14.                                 var digit1 = parseFloat(number1);
  15.                                 var digit2 = parseFloat(number2);
  16.                                 var total = digit1 - digit2;
  17.                                 document.getElementById('result').innerHTML = "<h2>The answer is: "+total+"</h2>";
  18.                         break;
  19.                         case "mult":
  20.                                 var digit1 = parseFloat(number1);
  21.                                 var digit2 = parseFloat(number2);
  22.                                 var total = digit1 * digit2;
  23.                                 document.getElementById('result').innerHTML = "<h2>The answer is: "+total+"</h2>";
  24.                         break;
  25.                         case "div":
  26.                                 var digit1 = parseFloat(number1);
  27.                                 var digit2 = parseFloat(number2);
  28.                                 var total = digit1 / digit2;
  29.                                 document.getElementById('result').innerHTML = "<h2>The answer is: "+total+"</h2>";
  30.                         break;
  31.                 }
  32.         }else{
  33.                 document.getElementById('result').innerHTML = '';
  34.         }
  35. }
There you have it we successfully created a Auto Calculate Total using JavaScript. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

Add new comment