Simple calculator in HTML and JavaScript

simple javascript calculator form
javascript calculator function
simple calculator using javascript w3schools
calculator in html code w3schools
html code for scientific calculator
how to make a calculator using html, css and javascript
simple calculator in javascript using switch case
javascript calculator object

I have to write a calculator in HTML. I really can't find what is going wrong and it does not show the results. I can't find something wrong can you help? I'm running it in Chrome.

JavaScript File and and the HTML:

showresult(choise){
var n1=parsefloat(document.getElementById('num1').value);
var n2=parsefloat(document.getElementById('num2').value);
var r;
var c=choise;

switch(c)
	{
	case '1':
		r=n1+n2;
		break;
	case '2':
		r=n1-n2;
		break;
	case '3':
		r=n1*n2;
		break;
	case '4': 
		r=n1/n2;
		break;
	case '5':
		r=n2*100/n1;
		break;
	default:
		break;
			
	}
document.getElementById('result').innerHTML=r;

	

}
<!DOCTYPE html>
<html>
<head>
<title>Calculator</title>
<script src="calculator.js" type="text/javascript"></script>
</head>
<body>


<h1>My calculator</h1>
<table border="1" cellpadding="5" cellspacing="5" width="600">
<tr align="center">
<td>First number</td>
<td>Second Number</td>
<td>Result</td>
</tr>
<tr align="center">
<td><input name="number1" type="text" size=10 id='num1'/></td>
<td><input name="number2" type="text" size=10 id='num2'/></td>
<td> <input type="text" id='result' readonly ></td>
</tr>
<tr>
<td colspan="3">
<button onclick="showresult('1')">+</button>
<button onclick="showresult('2')">-</button>
<button onclick="showresult('3')">*</button>
<button onclick="showresult('4')">/</button>
<button onclick="showresult('5')">%</button>
</td>
</tr>

</table>
</body>
</html>

Things to fix: 1) Assign to value, not innerHTML, when referring to an input element. (They have no content, hence no innerHTML. 2) Start a function declaration with the keyword function. 3) It’s parseFloat, not parsefloat. JavaScript is case-sensitive.

Minimally fixed code:

function showresult(choise){
var n1=parseFloat(document.getElementById('num1').value);
var n2=parseFloat(document.getElementById('num2').value);
var r;
var c=choise;

switch(c)
	{
	case '1':
		r=n1+n2;
		break;
	case '2':
		r=n1-n2;
		break;
	case '3':
		r=n1*n2;
		break;
	case '4': 
		r=n1/n2;
		break;
	case '5':
		r=n2*100/n1;
		break;
	default:
		break;
			
	}
document.getElementById('result').value=r;

	

}
<!DOCTYPE html>
<html>
<head>
<title>Calculator</title>
<script src="calculator.js" type="text/javascript"></script>
</head>
<body>


<h1>My calculator</h1>
<table border="1" cellpadding="5" cellspacing="5" width="600">
<tr align="center">
<td>First number</td>
<td>Second Number</td>
<td>Result</td>
</tr>
<tr align="center">
<td><input name="number1" type="text" size=10 id='num1'/></td>
<td><input name="number2" type="text" size=10 id='num2'/></td>
<td> <input type="text" id='result' readonly ></td>
</tr>
<tr>
<td colspan="3">
<button onclick="showresult('1')">+</button>
<button onclick="showresult('2')">-</button>
<button onclick="showresult('3')">*</button>
<button onclick="showresult('4')">/</button>
<button onclick="showresult('5')">%</button>
</td>
</tr>

</table>
</body>
</html>

Assign OnClick event for all the buttons having numbers and arithmetic operators. JavaScript for simple JS calculator Once you have all these files saved in the same folder, it’s time to test out your work. Launch the “index.html” file in your favorite browser and test your stunning looking JS, HTML and CSS written calculator. Wanna save yourself the stress of having to write and save these codes yourself?

Make sure you declare the function correctly.

function showresult(choise) {
/* ... */
}

Also, remember that Javascript is case sensitive therefore the right way to parse a float is parseFloat() Lastly, as pointed out by gillesc, use the attribute value on the input element. document.getElementById('result').value=r;

In this series, we are going to be making a simple calculator with basic HTML, CSS and JavaScript. Our calculator will only able to perform  Steps to create a Simple calculator Using HTML and JavaScript 1. At first Insert a <form> element within <body> tag. 2. Create a table using <table> ..</table> tag. 3. Insert two types of Input text and button within table data of table row using 4. Assign OnClick event for all the buttons

you should replace "innerHTML" to "value" in your code.

If you want to access the text within a non-input HTML element, then you are going to have to use the innerHTML property instead of value

Now code becomes:

document.getElementById('result').value=r;

Here we used the value property that all input elements have to use to grab the value the user enters.

Things to fix: 1) Assign to value , not innerHTML , when referring to an input element. (They have no content, hence no innerHTML . 2) Start a  Simple calculator in HTML and JavaScript. Ask Question Asked 5 years, 5 months ago. Simple calculator on "html" or JS? – Diego Cardenas Nov 24 '14 at 18:17.

You can use this in your code

function calc(form) {
      var D = "0";
      var A = document.getElementById("num1").value;
      var B = document.getElementById("op").value;
      var C = document.getElementById("num2").value;

      switch(B){
          case'+':
          D= parseInt(A)+parseInt(C); break;
          case'-':
          D= parseInt(A)-parseInt(C); break;
          case'*':
          D=parseInt(A)*parseInt(C); break;
          case'/':
          D=parseInt(A)/parseInt(C); break;
      }
}

js in React Project ? Color Psychology in front-end development · Node.js | Callback Concept · Difference between <div> and <span> Tag in HTML. Basic JavaScript Calculator Source Code Given Here Below. Copy these codes and save it with the name that is given here. use can use IDE as well as notepad in windows. I would recommend that first understand this code and then use it anywhere.

<script>

	var num1;
	var op;
	
	function pressBtn(value){
		var prev = document.forms["calc"]["display"].value;
		if (!(prev == "" && value==0)){
			var newval = prev+value;
			document.forms["calc"]["display"].value = newval;
		}
		 
	}
	
	function pressOP(operator){
		op = operator;
		var display = document.forms["calc"]["display"].value;
		num1 = parseInt(display);
		document.forms["calc"]["display"].value = "";
	}
	
	function pressEq(){
		var display = document.forms["calc"]["display"].value;
		var num2 = parseInt(display);
		switch(op){
			case "P":
				var ans=num1+num2;
				break;
			case "M":
				var ans=num1-num2;
				break;	
		}
		document.forms["calc"]["display"].value = ans;
	}
</script>
<style>
	input[type=button]{
		width: 100px;
		height: 100px;
		font-size: 36px;
		margin: 5px;
	}	
	input[type=text]{
		width: 300px;
		height: 75px;
		font-size: 48px;
		margin: 5px;
		text-align: right;
	}
</style>
<body style="background: #ccc;">
	<div style="width: 500px; margin: auto; background: #fff; text-align: center">
		<form name="calc">
			<input type="text" name="display" readonly ><br>
			<input type="button" name="btn7" value="7" onClick="pressBtn(7)">
			<input type="button" name="btn8" value="8" onClick="pressBtn(8)">
			<input type="button" name="btn9" value="9" onClick="pressBtn(9)"><br>
			<input type="button" name="btn4" value="4" onClick="pressBtn(4)">
			<input type="button" name="btn5" value="5" onClick="pressBtn(5)">
			<input type="button" name="btn6" value="6" onClick="pressBtn(6)"><br>
			<input type="button" name="btn1" value="1" onClick="pressBtn(1)">
			<input type="button" name="btn2" value="2" onClick="pressBtn(2)">
			<input type="button" name="btn3" value="3" onClick="pressBtn(3)"><br>
			<input type="button" name="btn0" value="0" onClick="pressBtn(0)">
            <input type="button" name="btnPlus" value="+" onClick="pressOP('P')" >
            <input type="button" name="btnPlus" value="-" onClick="pressOP('M')" ><br>
            <input type="button" name="btnEq" value="=" onClick="pressEq();" >
		</form>
	</div>
</body>

<title>Calculator - Gheorghe Marin. Adrian</title>. </head>. <body>. <div id='calc​-contain'>. <form name="calculator">. <input type="text" name="answer" />. In this video, I’ll show you how to create a simple calculator using JavaScipt! Developing a calculator isn’t easy, but I make this super simple by using the eval () function. (The argument of this function is a string, and it executes a string as JavaScript code. Hope you enjoy coding! Working as a freelance programmer in Japan.

This is a simple calculator tutorial using HTML, CSS and Javascript. The logic is very simple Duration: 20:45 Posted: May 17, 2018 Here, I created the div ( .box) which represent the structure of calculator. Inside it, I created two div tags, one for display (inside this, I added input type="text" to display the result and set this to read only) and one for Keys. Here, you see some onclick event about which I will talk later. Preview (Using the Above Code Calculator looks

I added the button in html next to the AC button but How would you go about creating the Duration: 6:41 Posted: Feb 5, 2017 Here a Calculator is going to be formed with HTML code. Calculator Title: This is the title at the top of our application, “GeeksforGeeks Calculator”. Output Screen: This will be our output screen, where all text will be shown. Like the input that the user will type and the answer calculated from the user input.

For creating a basic calculator in JavaScript, we use table structure, input type button and <html> <head> <script> function d(val){ document. Let’s start by creating an object to help us keep track of these values. Type this at the top of the JavaScript pane in JSFiddle: const calculator = {displayValue: ' 0 ', firstOperand: null, waitingForSecondOperand: false, operator: null,}; The calculator object consists of everything that we need to construct a valid expression:

Comments