In Association with Amazon.in   Flipkart

Tuesday, November 19, 2013

JavaScript DOM By ID Example For Standard Calculator

JavaScript Program For Standard Calculator:

<head>
<script>

function calc(k)
{
if(k=="+" || k=='-' || k=='*' || k=='/')
{
v1=parseInt(document.getElementById('res').value);
op=k;
document.getElementById('res').value="";
}
else if(k=="=")
{
v2=parseInt(document.getElementById('res').value);
document.getElementById('res').value="";
if(op=='+')
ans=(v1+v2);
else if(op=='-')
ans=(v1-v2);
else if(op=='*')
ans=(v1*v2);
else
ans=(v1/v2);
document.getElementById('res').value=ans;
}
else
{
no=document.getElementById('res').value;
if(no)
document.getElementById('res').value=no+k;
else
document.getElementById('res').value=k;
}
}
</script>
</head>

<body>
<form>
<input type="text" name="result" id="res" readonly > <br>
<input type="button" onClick="calc(1);" value="1">
<input type="button" onClick="calc(2);" value="2">
<input type="button" onClick="calc(3);" value="3">
<br/>
<input type="button" onClick="calc(4);" value="4">
<input type="button" onClick="calc(5);" value="5">
<input type="button" onClick="calc(6);" value="6">
<br/>
<input type="button" onClick="calc(7);" value="7">
<input type="button" onClick="calc(8);" value="8">
<input type="button" onClick="calc(9);" value="9">
<input type="button" onClick="calc(0);" value="0">
<br/>
<input type="button" onClick="calc('+');" value="+">
<input type="button" onClick="calc('-');" value="-">
<input type="button" onClick="calc('*');" value="*">
<input type="button" onClick="calc('/');" value="/">
<br/>
<input type="button" onClick="calc('=');" value="=">
<input type="reset" value="Clear">
</form>
</body>


Output:






No comments:

Post a Comment


Related Posts Plugin for WordPress, Blogger...