PDA

View Full Version : Focus won't work



waynefrank
05-05-2005, 09:18 PM
The focus statement, right below the ??? comment will not work. What am I doing wrong ?? Thanks

<html>
<head>
<title>Mortgage Calculator</title>
<style>

h1 {color:black; font-family:Arial; text-align:center; }

table {color:black; font-family:Arial; font-size:18;}

</style>

<script>

function Monthly(I, N, S) {
return (S*I/12*Math.pow(I/12+1,N))/(Math.pow(I/12+1,N)-1);
} // end function Monthly(I, N, S)

function ShowVal() {

var SS = eval(document.mortgage_form.LoanAmount.value);
/* alert("SS =" + SS); */

var NN = eval(document.mortgage_form.TotalNumberMonths.value);
/* alert("NN =" + NN); */

var II = eval(document.mortgage_form.YearlyInterestRate.value);
/* alert("II =" + II); */

var TP = Monthly(II, NN, SS);


document.mortgage_form.MonthlyPayment.value = TP;

document.mortgage_form.TotalOfPayments.value = TP * NN;

} // end function ShowVal()

function CheckInterest() {
var Int = eval(document.mortgage_form.YearlyInterestRate.value);
/* alert("CheckInterest Int =" + Int); */


if(Int <= 0 || Int >= 1) {

alert("Fix the interest, you fool");

document.mortgage_form.YearlyInterestRate.value = "";

/* ?????? */
document.mortgage_form.YearlyInterestRate.focus();


} // end if


} // end function CheckInterest()


</script>


</head>

<body>
<h1> Mortgage Calculator</h1>

<table width="60%" align="center">
<tr><td>
Enter the yearly interest rate as a decimal, the total number of monthly payments,
and the amount of the mortgage.
Click the Calculate button to view the monthly payment.
Click the Reset button to reset the form.
</td></tr>
</table>


<form name="mortgage_form"><center>
<table>
<tr>
<td>Loan Amount</td>
<td><input class="number" name="LoanAmount" size="15" maxlength="15" value="">
</td>
</tr>

<tr>
<td>Yearly Interest Rate</td>
<td><input class="number" name="YearlyInterestRate" size="15" maxlength="15" value="" onchange="CheckInterest()"></td>
</tr>

<tr>
<td>Total Number of Months</td>
<td><input class="number" name="TotalNumberMonths" size="15" maxlength="15" value=""></td>
</tr>

<tr>
<td> <input type="button" value="Calculate" onclick="ShowVal()"></td>

<td> <input type="reset" value="Reset"></td>
</tr>
<tr>
<td>Monthly Payment</td>
<td><input class="number" name="MonthlyPayment" size="15" maxlength="15" value="" disabled></td>
</tr>

<tr>
<td>Total of Payments</td>
<td><input class="number" name="TotalOfPayments" size="15" maxlength="15" value="" disabled></td>
</tr>



</table>
</center></form>

</body>


</html>

jscheuer1
05-06-2005, 09:32 AM
Don't ask me why but, use onBlur instead of onchange here:


<td><input class="number" name="YearlyInterestRate" size="15" maxlength="15" value="" onBlur="CheckInterest()"></td>Only thing I can figure is, onchange occurs while focus is still on the element.

jscheuer1
05-06-2005, 10:01 AM
I was so pleased with myself for getting that to work, I neglected to test it in Mozilla, it works in IE but not in NS7.2

This works in both:


setTimeout("document.mortgage_form.YearlyInterestRate.focus()",20);Substitute that for your dis-functional line. Using this code, it makes no difference if onBlur or onChange is used in the HTML section.

waynefrank
05-06-2005, 01:28 PM
Thanks.