Simple Calculator in HTML Code

Simple Calculator in HTML Code
Code Snippet:JavaScript Calculator
Author: Vikas Lalwani
Published: January 11, 2024
Last Updated: January 22, 2024
Downloads: 13,137
License: MIT
Edit Code online: View on CodePen
Read More

This code snippet helps you to create a simple calculator in HTML. It comes with a flat UI design with all basic calculation methods. Users can easily do addition, subtraction, multiplication, and division operations on numbers.

This simple calculator comes with a virtual Numpad. It doesn’t support keyboard keys to enter numbers. However, you can use the mouse (or tap) to enter numbers to perform mathematics operations. Moreover, you can highly customize it with additional CSS.

How to Create Simple HTML Calculator

1. First of all, create the HTML structure for the calculator as follows:

  1. <div class="calculator">
  2. <div class="input" id="input"></div>
  3. <div class="buttons">
  4. <div class="operators">
  5. <div>+</div>
  6. <div>-</div>
  7. <div>×</div>
  8. <div>÷</div>
  9. </div>
  10. <div class="leftPanel">
  11. <div class="numbers">
  12. <div>7</div>
  13. <div>8</div>
  14. <div>9</div>
  15. </div>
  16. <div class="numbers">
  17. <div>4</div>
  18. <div>5</div>
  19. <div>6</div>
  20. </div>
  21. <div class="numbers">
  22. <div>1</div>
  23. <div>2</div>
  24. <div>3</div>
  25. </div>
  26. <div class="numbers">
  27. <div>0</div>
  28. <div>.</div>
  29. <div id="clear">C</div>
  30. </div>
  31. </div>
  32. <div class="equal" id="result">=</div>
  33. </div>
  34. </div>

2. After that, style the calculator interface using the following CSS.

  1. .calculator {
  2. padding: 20px;
  3. max-width: 440px;
  4. margin: 10px auto;
  5. -webkit-box-shadow: 0px 1px 4px 0px rgba(0, 0, 0, 0.2);
  6. box-shadow: 0px 1px 4px 0px rgba(0, 0, 0, 0.2);
  7. border-radius: 1px;
  8. }
  9. .input {
  10. border: 1px solid #ddd;
  11. border-radius: 1px;
  12. height: 60px;
  13. padding-right: 15px;
  14. padding-top: 10px;
  15. text-align: right;
  16. margin-right: 6px;
  17. font-size: 2.5rem;
  18. overflow-x: auto;
  19. transition: all .2s ease-in-out;
  20. }
  21. .input:hover {
  22. border: 1px solid #bbb;
  23. -webkit-box-shadow: inset 0px 1px 4px 0px rgba(0, 0, 0, 0.2);
  24. box-shadow: inset 0px 1px 4px 0px rgba(0, 0, 0, 0.2);
  25. }
  26. .operators div {
  27. display: inline-block;
  28. border: 1px solid #bbb;
  29. border-radius: 1px;
  30. width: 80px;
  31. text-align: center;
  32. padding: 10px;
  33. margin: 20px 4px 10px 0;
  34. cursor: pointer;
  35. background-color: #ddd;
  36. transition: border-color .2s ease-in-out, background-color .2s, box-shadow .2s;
  37. }
  38. .operators div:hover {
  39. background-color: #ddd;
  40. -webkit-box-shadow: 0px 1px 4px 0px rgba(0, 0, 0, 0.2);
  41. box-shadow: 0px 1px 4px 0px rgba(0, 0, 0, 0.2);
  42. border-color: #aaa;
  43. }
  44. .operators div:active {
  45. font-weight: bold;
  46. }
  47. .leftPanel {
  48. display: inline-block;
  49. }
  50. .numbers div {
  51. display: inline-block;
  52. border: 1px solid #ddd;
  53. border-radius: 1px;
  54. width: 80px;
  55. text-align: center;
  56. padding: 10px;
  57. margin: 10px 4px 10px 0;
  58. cursor: pointer;
  59. background-color: #f9f9f9;
  60. transition: border-color .2s ease-in-out, background-color .2s, box-shadow .2s;
  61. }
  62. .numbers div:hover {
  63. background-color: #f1f1f1;
  64. -webkit-box-shadow: 0px 1px 4px 0px rgba(0, 0, 0, 0.2);
  65. box-shadow: 0px 1px 4px 0px rgba(0, 0, 0, 0.2);
  66. border-color: #bbb;
  67. }
  68. .numbers div:active {
  69. font-weight: bold;
  70. }
  71. div.equal {
  72. display: inline-block;
  73. border: 1px solid #3079ED;
  74. border-radius: 1px;
  75. width: 17%;
  76. text-align: center;
  77. padding: 127px 10px;
  78. margin: 10px 6px 10px 0;
  79. vertical-align: top;
  80. cursor: pointer;
  81. color: #FFF;
  82. background-color: #4d90fe;
  83. transition: all .2s ease-in-out;
  84. }
  85. div.equal:hover {
  86. background-color: #307CF9;
  87. -webkit-box-shadow: 0px 1px 4px 0px rgba(0, 0, 0, 0.2);
  88. box-shadow: 0px 1px 4px 0px rgba(0, 0, 0, 0.2);
  89. border-color: #1857BB;
  90. }
  91. div.equal:active {
  92. font-weight: bold;
  93. }

3. Finally, add the following JavaScript code before closing the body tag and done.

  1. "use strict";
  2.  
  3. var input = document.getElementById('input'), // input/output button
  4. number = document.querySelectorAll('.numbers div'), // number buttons
  5. operator = document.querySelectorAll('.operators div'), // operator buttons
  6. result = document.getElementById('result'), // equal button
  7. clear = document.getElementById('clear'), // clear button
  8. resultDisplayed = false; // flag to keep an eye on what output is displayed
  9.  
  10. // adding click handlers to number buttons
  11. for (var i = 0; i < number.length; i++) {
  12. number[i].addEventListener("click", function(e) {
  13.  
  14. // storing current input string and its last character in variables - used later
  15. var currentString = input.innerHTML;
  16. var lastChar = currentString[currentString.length - 1];
  17.  
  18. // if result is not diplayed, just keep adding
  19. if (resultDisplayed === false) {
  20. input.innerHTML += e.target.innerHTML;
  21. } else if (resultDisplayed === true && lastChar === "+" || lastChar === "-" || lastChar === "×" || lastChar === "÷") {
  22. // if result is currently displayed and user pressed an operator
  23. // we need to keep on adding to the string for next operation
  24. resultDisplayed = false;
  25. input.innerHTML += e.target.innerHTML;
  26. } else {
  27. // if result is currently displayed and user pressed a number
  28. // we need clear the input string and add the new input to start the new opration
  29. resultDisplayed = false;
  30. input.innerHTML = "";
  31. input.innerHTML += e.target.innerHTML;
  32. }
  33.  
  34. });
  35. }
  36.  
  37. // adding click handlers to number buttons
  38. for (var i = 0; i < operator.length; i++) {
  39. operator[i].addEventListener("click", function(e) {
  40.  
  41. // storing current input string and its last character in variables - used later
  42. var currentString = input.innerHTML;
  43. var lastChar = currentString[currentString.length - 1];
  44.  
  45. // if last character entered is an operator, replace it with the currently pressed one
  46. if (lastChar === "+" || lastChar === "-" || lastChar === "×" || lastChar === "÷") {
  47. var newString = currentString.substring(0, currentString.length - 1) + e.target.innerHTML;
  48. input.innerHTML = newString;
  49. } else if (currentString.length == 0) {
  50. // if first key pressed is an opearator, don't do anything
  51. console.log("enter a number first");
  52. } else {
  53. // else just add the operator pressed to the input
  54. input.innerHTML += e.target.innerHTML;
  55. }
  56.  
  57. });
  58. }
  59.  
  60. // on click of 'equal' button
  61. result.addEventListener("click", function() {
  62.  
  63. // this is the string that we will be processing eg. -10+26+33-56*34/23
  64. var inputString = input.innerHTML;
  65.  
  66. // forming an array of numbers. eg for above string it will be: numbers = ["10", "26", "33", "56", "34", "23"]
  67. var numbers = inputString.split(/\+|\-|\×|\÷/g);
  68.  
  69. // forming an array of operators. for above string it will be: operators = ["+", "+", "-", "*", "/"]
  70. // first we replace all the numbers and dot with empty string and then split
  71. var operators = inputString.replace(/[0-9]|\./g, "").split("");
  72.  
  73. console.log(inputString);
  74. console.log(operators);
  75. console.log(numbers);
  76. console.log("----------------------------");
  77.  
  78. // now we are looping through the array and doing one operation at a time.
  79. // first divide, then multiply, then subtraction and then addition
  80. // as we move we are alterning the original numbers and operators array
  81. // the final element remaining in the array will be the output
  82.  
  83. var divide = operators.indexOf("÷");
  84. while (divide != -1) {
  85. numbers.splice(divide, 2, numbers[divide] / numbers[divide + 1]);
  86. operators.splice(divide, 1);
  87. divide = operators.indexOf("÷");
  88. }
  89.  
  90. var multiply = operators.indexOf("×");
  91. while (multiply != -1) {
  92. numbers.splice(multiply, 2, numbers[multiply] * numbers[multiply + 1]);
  93. operators.splice(multiply, 1);
  94. multiply = operators.indexOf("×");
  95. }
  96.  
  97. var subtract = operators.indexOf("-");
  98. while (subtract != -1) {
  99. numbers.splice(subtract, 2, numbers[subtract] - numbers[subtract + 1]);
  100. operators.splice(subtract, 1);
  101. subtract = operators.indexOf("-");
  102. }
  103.  
  104. var add = operators.indexOf("+");
  105. while (add != -1) {
  106. // using parseFloat is necessary, otherwise it will result in string concatenation :)
  107. numbers.splice(add, 2, parseFloat(numbers[add]) + parseFloat(numbers[add + 1]));
  108. operators.splice(add, 1);
  109. add = operators.indexOf("+");
  110. }
  111.  
  112. input.innerHTML = numbers[0]; // displaying the output
  113.  
  114. resultDisplayed = true; // turning flag if result is displayed
  115. });
  116.  
  117. // clearing the input on press of clear
  118. clear.addEventListener("click", function() {
  119. input.innerHTML = "";
  120. })

That’s all! hopefully, you have successfully created a simple calculator using this HTML code snippet. If you have any questions or suggestions, let me know by comment below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About CodeHim

Free Web Design Code & Scripts - CodeHim is one of the BEST developer websites that provide web designers and developers with a simple way to preview and download a variety of free code & scripts. All codes published on CodeHim are open source, distributed under OSD-compliant license which grants all the rights to use, study, change and share the software in modified and unmodified form. Before publishing, we test and review each code snippet to avoid errors, but we cannot warrant the full correctness of all content. All trademarks, trade names, logos, and icons are the property of their respective owners... find out more...

Please Rel0ad/PressF5 this page if you can't click the download/preview link

X