Web Analytics Made Easy -
StatCounter Problem with focus() - CodingForum

Announcement

Collapse
No announcement yet.

Problem with focus()

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new 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
    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. ;)

    Comment


    • #3
      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?

      Comment


      • #4
        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/

        Comment

        Working...
        X