I am trying to allow Javascript to calculate a function of several different variables and results. Honestly I’ve spent the last two days working on it and literally have no idea where to go from here. My code as follows:
<html>
<title>Calculate Your Monthly Payments</title>
<script language = "JavaScript">
function Calc(){
var a, res;
a = parseFloat(document.monthly.form.value);
res1 = a/1000;
res2 = a/500;
res3 = a/300;
if(document.monthly.payments[1].style.visibility = "visible" )
window.alert("Your monthly repayments have been estimaded to be £" + res1);
else if(document.monthly.payments[2].style.visibility = "visible" )
window.alert("Your monthly repayments have been estimaded to be £ " + res2);
else(document.monthly.payments[3].style.visibility = "visible" )
window.alert("Your monthly repayments have been estimaded to be £ " + res3);
}
</script>
</head>
<div id="container">
<h2>Calculate Your Monthly Payments</h2>
<h4>Please fill in the form below</h4>
<form name = "monthly">
How much are you borrowing? (£): <br><input name = "form" type = "text" size = 10 id="textarea"> <br><br>
Mortgage Type: <br>
<select name = "payments">
<option value="" selected>Please Select </option>
<option value="1">Mortgage 2.19% fixed until 2015 </option>
<option value="2">Mortgage 3.17% fixed until 2017 </option>
<option value="3">Mortgage 3.18% fixed until 2024 </option>
</select>
<br><br>
<input type = "button" value = "Calculate Payments! " onclick = "Calc();">
</form>
</div>
</html>
I have been trying to have the event being displayed as a popup which says
“Your Monthly Payment is …”.
The equation I want to implement should work as whatever you enter is divided by what you choose.
If a is selected then as an example it will be divided by 1000
if b is selected then as an example it will be divided by 500
and if c is selected then as an example it will be divided by 300.
However.. none of this works.
I have been going though the code over and over and have made it work, though only partially as the event then displays two pop-ups. First being option value 1 and second replacing it after being click with option value 2.
The Problem is that:
Upon viewing in a browser and clicking on the “Calculate Payments” button nothing happens. Why is this?
I hope I made this clear since I’m a bit rough with English but I would appreciate any help whatsoever.
This works – try it.