In Association with Amazon.in   Flipkart

Tuesday, November 19, 2013

JavaScript DOM By Id Example To Calculate Two Values

Example: 
<head>
<script>

function calc(ch)
{
x=document.getElementById('v1').value;
x=parseFloat(x);
y=document.getElementById('v2').value;
y=parseFloat(y);
switch(ch)
{
case '+' : a=(x+y); break;
case '-' : a=x-y; break;
case '*' : a=x*y; break;
case '/' : a=x/y; break;
default: a="Invalid Input";
}
document.getElementById("result").value=a;
document.getElementById("result").style.color="green";
document.getElementById("result").style.backgroundColor="pink";
}

</script>
</head>

<body>
<form>
Enter Value1:
<input type="text" name="v1" id="v1"> <br>
Enter Value2:
<input type="text" name="v2" id="v2"> <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="/">
<input type="reset" value="Clear" />
<br/>
Result: <input type="text" name="res" id="result" readonly> <br>
</form>
</body>


Output:


Enter Value1:
Enter Value2:

Result:

No comments:

Post a Comment


Related Posts Plugin for WordPress, Blogger...