Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jun 2002
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Problem with focus()

    My HTML has an input text field that calls a javascript function when the field contents change (onChange).


    The javascript function validates the data and if the data is in error sets the focus to the field with the error and returns false.

    In NN 6.2 after clicking the "OK" in the alert error message box the cursor stays on the field in error for about 3 seconds and then jumps to the next input text field. How do I keep the cursor on the field in error so the user can correct the data?

    In IE 5.5 it works incorrectly also, the cursor jumps to the next field right away.

    In NN 4.7 it works correctly, the cursor stays on the field in error.

    Here is my code

    <td align="left" valign="top"><font size="4"><input type="text" name="application" value="" size=2 maxlength=3 onChange="return checkApplicationCode(this.value, this.name)">
    </td>


    function checkApplicationCode (s, n) {

    if ((s != "lv") && (s != "LV") &&
    (s != "tc") && (s != "TC"))
    {
    alert(n + " must be LV or TC. Please correct.");
    window.document.forms[0].application.focus();
    return false;
    }
    }


    I appreciate any help you can give me. Thanks.

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    41 8' 52" N -95 53' 31" W
    Posts
    3,660
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Without examining the code in detail, I'm wondering if perhaps the two different text fields erroneously have the same name?
    Former ASP Forum Moderator - I'm back!

    If you can teach yourself how to learn, you can learn anything. ;)

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Location
    The Planet Earth Code Poet: True
    Posts
    282
    Thanks
    0
    Thanked 1 Time in 1 Post
    I beleive this has to do with the way the onchange event "bubbles" in different browsers. I will be the first to admit that I am not strong at event bubbling functions so I would try a differen event handler. This test scenario worked out:

    <form>
    <input type="text" name="appCode" value="" size="2" maxlength="3" onkeyup="if(this.value.length==2)checkApplicationCode(this.value, this.name)" />
    <input type="text" name="nextfield" value="" size="2" maxlength="3" />
    </form>
    <script>
    function checkApplicationCode (s, n) {
    if ((s.toLowerCase()!="lv") && (s.toLowerCase()!="tc")) {
    alert(n + " must be LV or TC. Please correct.");
    document.forms[0][n].focus();
    }
    }
    </script>

    By the way, why would you have a maxlength of 3 if the input must be 2 chartcers long?

  • #4
    New Coder
    Join Date
    Jun 2002
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi

    your code appears correct. No reason the focus should switch on to another element.
    Therefore, there must be some other section of your code which might interfere, try posting a few more snippets, little by little we can find the possible error

    ciao
    Alberto http://www.unitedscripters.com/


  •  

    Posting Permissions

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