Tuesday, 17 September 2024

WRITE A JAVASCRIPT TO PERFORM AIRTHMETIC OPERATION ON BUTTON CLICK EVENT

 




<html>

<head><title>Example-8</title>

<script language="JavaScript">

function add() {

var num1=document.getElementById("txt_num1").value;

var num2=document.getElementById("txt_num2").value;

var n1=parseInt(num1);

var n2=parseInt(num2);

var ans=n1+n2;

document.getElementById("txt_res").value=ans;

}

function sub() {

var num1=document.getElementById("txt_num1").value;

var num2=document.getElementById("txt_num2").value;

var n1=parseInt(num1);

var n2=parseInt(num2);

var ans=n1-n2;

document.getElementById("txt_res").value=ans;

}

function mul() {

var num1=document.getElementById("txt_num1").value;

var num2=document.getElementById("txt_num2").value;

var n1=parseInt(num1);

var n2=parseInt(num2);

var ans=n1*n2;

document.getElementById("txt_res").value=ans;

}

function div() {

var num1=document.getElementById("txt_num1").value;

var num2=document.getElementById("txt_num2").value;

var n1=parseInt(num1);

var n2=parseInt(num2);

var ans=n2/n1;

document.getElementById("txt_res").value=ans;

}

</script>

</head>


<body>

<form method="POST">

<table border="2px">

<tr>

<th>Number1:</th>

<td><input type="text" id="txt_num1"/></td>

</tr>

<tr>

<th>Number2:</th>

<td><input type="text" id="txt_num2"/></td>

</tr>

<tr>

<td colspan="2" align="center"><input type="button" value="ADD"

onclick="add()"/>

<input type="button" value="SUB" onclick="sub()"/>

<input type="button" value="MUL" onclick="mul()"/>

<input type="button" value="DIV" onclick="div()"/></td>

</tr>

<tr>

<th>RESULT:</th>

<td><input type="text" id="txt_res"/></td>

</tr>

</table>

</form>

</body>

</html>

No comments:

Post a Comment