Results 1 to 4 of 4

Thread: Focus won't work

  1. #1
    Join Date
    Apr 2005
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Focus won't work

    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>

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Don't ask me why but, use onBlur instead of onchange here:

    Code:
    <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.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    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:

    Code:
    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.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  4. #4
    Join Date
    Apr 2005
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •