Thread: Resolved HTML form and Javascript
View Single Post
Old 11-12-2012, 07:50 AM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,037
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Your main problems are that you are trying to get the values of p, n and r as soon as the page loads (when they do not even exist at that moment) instead of when the function runs.
Also, the values calculated by one function are not somehow retained for use by the second function - you must work out the interest again. Once a function ends the values of any local variables ae lost.
Also, values entered by the user are strings unless converted to numbers.

Code:
<html>
<head>Calculator Simple Interest
<script>
function si() {
var p=Number(document.getElementById("pa").value);
var n=Number(document.getElementById("ny").value);
var r=Number(document.getElementById("ri").value);
var s=p*n*r/100;
alert("Total Interest Amount is "+s.toFixed(2));
}
function ta() {
var p=Number(document.getElementById("pa").value);
var n=Number(document.getElementById("ny").value);
var r=Number(document.getElementById("ri").value);
var s=p*n*r/100;
var t = p+s;
alert("Total Amount Payable is "+t.toFixed(2));
}
</script>
</head>
<title>Simple Interest Calculator</title>
<body>
<form>
<b>Principal Amount</b><input type="number" id="pa">
<b>No. of years</b><input type="number" id="ny">
<b>Rate of interest</b><input type="number" id="ri">
<input type="button" onclick="si()" value="Calculate Interest">
<input type="button" onclick="ta()" value="Final Amount">
</form>
</body>
</html>
"What's the three words you never want to hear while making love? Honey, I'm home." - Ken Hammond.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Users who have thanked Philip M for this post:
adityavishnu (11-13-2012)