Implementing a simple calculator with javascript

Implementing a simple calculator with javascript

This article example shares the specific code of javascript to implement a simple calculator for your reference. The specific content is as follows

Design a simple calculator

Code

 <body>
  <a>First number</a>
  <input type="test" id="inputId1" value="" /><br/>
  <a>Second number</a>
  <input type="test" id="inputId2" value="" /><br/>
  <button onclick="cal('+')">+</button>
  <button onclick="cal('-')">-</button>
  <button onclick="cal('*')">*</button>
  <button onclick="cal('/')">/</button><br/>
  Calculation results
  <input type="test" id="resultId" value="" />
  <script type="text/javascript">
   // function add() {
   // console.log('add');
   // var inputObj1 = document.getElementById('inputId1');
   // var inputObj2 = document.getElementById('inputId2');
   // var result = parseInt(inputObj1.value) + parseInt(inputObj2.value);
   // var resultObj = document.getElementById('result');
   // resultObj.value = result;
   // console.log(result);
   // }
   function cal(type) {
    var inputObj1 = document.getElementById('inputId1');
    var inputObj2 = document.getElementById('inputId2');
    switch(type){
     case '+':
      var result = parseInt(inputObj1.value) + parseInt(inputObj2.value);
      break;
     case '-':
      var result = parseInt(inputObj1.value) - parseInt(inputObj2.value);
      break;
     case '*':
      var result = parseInt(inputObj1.value) * parseInt(inputObj2.value);
      break;
     case '/':
      var result = parseInt(inputObj1.value) / parseInt(inputObj2.value);
      break;
    }
    var resultObj = document.getElementById('resultId');
    resultObj.value = result;
   }
   
  </script>
 </body>
</html>

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Implementing a simple age calculator based on HTML+JS
  • Implementing a web calculator with native JavaScript
  • Detailed explanation of the process of realizing calculator function in javascript
  • JavaScript implements simple calculator function
  • js version to realize calculator function
  • Native js to implement a simple calculator
  • Writing a web calculator using javascript
  • JavaScript Example - Implementing a Calculator

<<:  Summary of basic SQL statements in MySQL database

>>:  How to run Python script on Docker

Recommend

MySQL changes the default engine and character set details

Table of contents 1. Database Engine 1.1 View dat...

How to safely shut down MySQL

When shutting down the MySQL server, various prob...

Code for aligning form checkbox and radio text

Alignment issues like type="radio" and t...

Linux CentOS MySQL database installation and configuration tutorial

Notes on installing MySQL database, share with ev...

Docker nginx example method to deploy multiple projects

Prerequisites 1. Docker has been installed on the...

MySQL kill command usage guide

KILL [CONNECTION | QUERY] processlist_id In MySQL...

Introduction to JavaScript Number and Math Objects

Table of contents 1. Number in JavaScript 2. Math...

Use pure CSS to create a pulsating loader effect source code

Effect Preview Press the "Click to Preview&q...

JavaScript custom plug-in to implement tab switching function

This article shares the specific code of JavaScri...

A brief introduction to MySQL dialect

Putting aside databases, what is dialect in life?...

Tutorial on installing VMWare15.5 under Linux

To install VMWare under Linux, you need to downlo...

HTML sample code for implementing tab switching

Tab switching is also a common technology in proj...

Solve the problem of Syn Flooding in MySQL database

Syn attack is the most common and most easily exp...

A brief discussion on whether too many MySQL data queries will cause OOM

Table of contents Impact of full table scan on th...