JavaScript - Simple Auto Calculate Number

Submitted by: 
Visitors have accessed this post 139 times.

In this tutorial we will create a Simple Auto Calculate Number using JavaScript. JavaScript is a scripting or programming language that allows you to implement complex things on web pages. It is a text-based programming language meant to run as part of a web-based application. It is an interpreted programming language that has a capabilities of Object-Oriented. So Let's do the coding...

Getting started:

This is the link for the bootstrap that i used for the layout design

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="<a href="">Sourcecodester</a>
  10. " rel="nofollow">">Sourcecodester</a>
  11. </a> </div>
  12. </nav>
  13. <div class="col-md-3"></div>
  14. <div class="col-md-6 well">
  15. <h3 class="text-primary">JavaScript - Simple Auto Calculate</h3>
  16. <hr style="border-top:1px dotted;"/>
  17. <div class="col-md-3"></div>
  18. <div class="col-md-6">
  19. <div class="form-group">
  20. <input type="number"class="form-control" id="number1" onkeyup="autoCalculate()" placeholder="Enter a number"/>
  21. </div>
  22. <div class="form-group">
  23. <select id="symbol" class="form-control" onchange="autoCalculate()">
  24. <option value="add">+</option>
  25. <option value="sub">-</option>
  26. <option value="mult">x</option>
  27. <option value="div">/</option>
  28. </select>
  29. </div>
  30. <div class="form-group">
  31. <input type="number"class="form-control" id="number2" onkeyup="autoCalculate()" placeholder="Enter a number"/>
  32. </div>
  33. <div class="form-group">
  34. <input type="text" class="form-control" id="answer" disabled="disabled"/>
  35. </div>
  36. </div>
  37. </div>
  38. <script src="js/script.js"></script>
  39. </body>
  40. </html>

Creating the Script

This code contains the script of the application. This code will calculate the number you have entered base on the equation you use. 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 folder.

  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('answer').value = total;
  12. break;
  13. case "sub":
  14. var digit1 = parseFloat(number1);
  15. var digit2 = parseFloat(number2);
  16. var total = digit1 - digit2;
  17. document.getElementById('answer').value = total;
  18. break;
  19. case "mult":
  20. var digit1 = parseFloat(number1);
  21. var digit2 = parseFloat(number2);
  22. var total = digit1 * digit2;
  23. document.getElementById('answer').value = total;
  24. break;
  25. case "div":
  26. var digit1 = parseFloat(number1);
  27. var digit2 = parseFloat(number2);
  28. var total = digit1 / digit2;
  29. document.getElementById('answer').value = total;
  30. break;
  31. }
  32. }else{
  33. document.getElementById('answer').value = '';
  34. }
  35. }

There you have it we successfully created a Simple Auto Calculate Number 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

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.