JavaScript implementation of a simple addition calculator

JavaScript implementation of a simple addition calculator

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

Specific requirements:

1. Page layout:
2. The calculation results also need to be displayed on the page after clicking the calculation button

Specific implementation:

<html>
  <head>
    <meta charset="utf-8">
    <title>Calculator</title>
    <script>
      function myck(type){
        var num1 = document.getElementById("num1");
        var num2 = document.getElementById("num2");
        if(type==1){
          // Calculation operation var result = parseInt(num1.value) + parseInt(num2.value);
          alert(result);
          document.getElementById("resultDiv").innerText="Final calculation result:"+
            result;
        }else if(type==2){
          if(confirm("Is it cleared correctly?")){
            // Clear num1.value = "";
            num2.value = "";
            document.getElementById("resultDiv").innerText="";
          }
        }
      }
    </script>
  </head>

  <body>
    <div style="margin-top: 100px;margin-left: 500px;">
      <span style="font-size: 60px;">Addition Calculator</span>   
    </div>
    <div>
      <div class="innerDiv" style="margin-left: 550px;">
        Number 1: <input id="num1" type="number" placeholder="Please enter number 1"> 
      </div>
    </div>
    <div>
      <div class="innerDiv" style="margin-left: 550px;">
      Number 2: <input id="num2" type="number" placeholder="Please enter number 2">
     </div> 
    </div>
    <div>
      <div style="margin-left: 600px;" class="innerDiv">
        <input type="button" onclick="myck(1)" value="Calculate">
        <input type="button" onclick="myck(2)" value="Clear">
      </div> 
    </div>
    <div id="resultDiv">

    </div>
  </body>
  <style>
    .innerDiv{
      margin-left: 420px;
      margin-top: 20px;
    }
  </style>
</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:
  • A simple calculator written in javascript, with a lot of content and practical methods. Recommended
  • Simple js code to realize calculator operation
  • js implements a simple calculator
  • HTML+JS implements simple calculator code (addition, subtraction, multiplication and division)
  • Simple calculator implementation code written in JS
  • javascript-simple calculator implementation step decomposition (with pictures)
  • Pure javascript code to implement calculator functions (three methods)
  • js implements a simple calculator
  • Web calculator A JS calculator
  • Simple implementation of js web calculator

<<:  Solution to the automatic stop of MySQL service

>>:  How to solve the problem that MySQL cannot start because it cannot create temporary files

Recommend

Comparing Document Locations

<br />A great blog post by PPK two years ago...

What does mysql database do?

MySQL is a relational database management system....

Detailed explanation of 8 ways to pass parameters in Vue routing components

When we develop a single-page application, someti...

A few experiences in self-cultivation of artists

As the company's influence grows and its prod...

Detailed explanation of how to use join to optimize SQL in MySQL

0. Prepare relevant tables for the following test...

Javascript to achieve drumming effect

This article shares the specific code of Javascri...

React's context and props explained

Table of contents 1. context 1. Usage scenarios 2...

Dockerfile text file usage example analysis

Dockerfile is a text file used to build an image....

How to simulate enumeration with JS

Preface In current JavaScript, there is no concep...

Summary of HTML knowledge points for the front end (recommended)

1. HTML Overview htyper text markup language Hype...

Implementation steps for setting up the React+Ant Design development environment

Basics 1. Use scaffolding to create a project and...

Implementation of CSS3 3D cool cube transformation animation

I love coding, it makes me happy! Hello everyone,...

Detailed explanation of MySQL remote connection permission

1. Log in to MySQL database mysql -u root -p View...