In this tutorial we will learn how to create a HTML file(add.html) and add two integer using input element (num1 and num2) and submit button. For calculation we will use pure JavaCript.
Create a file name add.html and copy the code given below, save and run in any browser.
This java script programme is extensively used in Jharkhand and West Bengal Btech CSE Lab.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Addition</title>
</head>
<body>
<table border="1">
<tr>
<td><label>Enter No. 1</label>
<input type="text" id="num1" value="0">
</td>
<td>
<label>Enter No. 2</label>
<input type="text" id="num2" value="0">
</td>
</tr>
<tr>
<td style="text-align:center">
<button onclick="calculate()">Calculate</button>
</td>
<td style="text-align:center">
<div id="result"></div>
</td>
</tr>
</table>
<script>
function calculate(){
var num1=parseInt(document.getElementById("num1").value);
var num2=parseInt(document.getElementById("num2").value);
var sum;
sum = num1+num2;
//alert(sum);
document.getElementById("result").innerHTML=sum;
}
</script>
</body>
</html>
Run this Programme :