In: Computer Science
Please create an Event-Based JavaScript Program of your choice. Please write new code for this project (starting from scratch). Thank you!!
Program:
<html>
<head>
<title>an Event-Based JavaScript Program</title>
<script language="javascript">
function Addition(frm)
{
var num1,num2,ans;
//first textbox value is assigned to num1
num1=eval(frm.txtNumber1.value);
//second textbox value is assigned to num2
num2=eval(frm.txtNumber2.value);
ans=num1+num2;
//result of addition is returned to button where the onclick event
is occured
return ans;
}
function Subtraction(frm)
{
var num1,num2,ans;
//first textbox value is assigned to num1
num1=eval(frm.txtNumber1.value);
//second textbox value is assigned to num2
num2=eval(frm.txtNumber2.value);
ans=num1-num2;
//result of Subtraction is returned to button where the onclick
event is occured
return ans;
}
function Multiplication(frm)
{
var num1,num2,ans;
//first textbox value is assigned to num1
num1=eval(frm.txtNumber1.value);
//second textbox value is assigned to num2
num2=eval(frm.txtNumber2.value);
ans=num1*num2;
//result of multiplication is returned to button where the onclick
event is occured
return ans;
}
function Division(frm)
{
var num1,num2,ans;
//first textbox value is assigned to num1
num1=eval(frm.txtNumber1.value);
//second textbox value is assigned to num2
num2=eval(frm.txtNumber2.value);
ans=parseFloat(num1/num2);
//result of division is returned to button where the onclick event
is occured
return ans;
}
</script>
</head>
<body>
<form name="frm">
Number 1: <input type="text"
name="txtNumber1"><br><br>
Number 2: <input type="text"
name="txtNumber2"><br><br>
<input type="button" value="Addition"
onclick="document.getElementById('result').innerHTML='Addition is
'+Addition(frm)">
<input type="button" value="Subtraction"
onclick="document.getElementById('result').innerHTML='Subtraction
is '+Subtraction(frm)">
<input type="button" value="Multiplication"
onclick="document.getElementById('result').innerHTML='Multiplication
is '+Multiplication(frm)">
<input type="button" value="Division"
onclick="document.getElementById('result').innerHTML='Division is
'+Division(frm)">
</form>
<h1 id="result"></h1>
</body>
</html>
Output: