Article

how to make a calculator in html

Yes, I can help you make a calculator in HTML. Here’s a basic example of a calculator:

htmlCopy code<!DOCTYPE html>
<html>
  <head>
    <title>Calculator</title>
  </head>
  <body>
    <h1>Calculator</h1>
    <form>
      <input type="text" id="result" name="result" value="0" readonly>
      <br>
      <input type="button" value="1" onclick="addToResult('1')">
      <input type="button" value="2" onclick="addToResult('2')">
      <input type="button" value="3" onclick="addToResult('3')">
      <input type="button" value="+" onclick="addToResult('+')">
      <br>
      <input type="button" value="4" onclick="addToResult('4')">
      <input type="button" value="5" onclick="addToResult('5')">
      <input type="button" value="6" onclick="addToResult('6')">
      <input type="button" value="-" onclick="addToResult('-')">
      <br>
      <input type="button" value="7" onclick="addToResult('7')">
      <input type="button" value="8" onclick="addToResult('8')">
      <input type="button" value="9" onclick="addToResult('9')">
      <input type="button" value="*" onclick="addToResult('*')">
      <br>
      <input type="button" value="0" onclick="addToResult('0')">
      <input type="button" value="." onclick="addToResult('.')">
      <input type="button" value="/" onclick="addToResult('/')">
      <input type="button" value="C" onclick="clearResult()">
      <br>
      <input type="button" value="=" onclick="calculateResult()">
    </form>
    
    <script>
      function addToResult(value) {
        document.getElementById("result").value += value;
      }
      
      function clearResult() {
        document.getElementById("result").value = "0";
      }
      
      function calculateResult() {
        var result = eval(document.getElementById("result").value);
        document.getElementById("result").value = result;
      }
    </script>
  </body>
</html>
Calculator

Calculator






This calculator includes basic arithmetic operations (addition, subtraction, multiplication, and division) as well as a clear button and an equals button. When a button is clicked, its corresponding value is added to the calculator’s input field. When the equals button is clicked, the input field’s value is evaluated using JavaScript’s eval() function, and the result is displayed in the input field.

Leave a Reply

Your email address will not be published. Required fields are marked *

How did rose state college respond to the shooting ?. © 2023 salve sys. Wo kann ich gute backlinks kaufen ?.