Directing the cursor to a input field

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • JW

    Directing the cursor to a input field

    hello everybody
    I've got the following problem. I'm writing a form and I'm using javascript
    to validate fields (i.e. numeric, not blank). When my scripts detects an
    error I want it to put the cursor in the corresponding field.
    I've tried the focus option, but that doesn't seem to work.
    This is the code I'm using:

    if (TestOk=="N")
    {
    alert('Veld mag niet leeg zijn');
    veld.select();
    veld.focus();
    }

    The veld.select() does select the error string, the focus, however doesm't
    seem to work.
    Anybody got a suggestion?
    Thanks anyway
    JW


  • Stuart Palmer

    #2
    Re: Directing the cursor to a input field

    > The veld.select() does select the error string, the focus, however doesm't[color=blue]
    > seem to work.[/color]

    Try document.Formna me.Fieldname.fo cus();

    where 'Formname' is the name of your form and 'Fieldname' is the name of the
    field you want to focus on.

    If that doesn't work (it should though),what error message do actually get?

    Thx
    Stu


    "JW" <janwouter@hotp op.com> wrote in message
    news:%ayBc.4629 $%27.144199@zon net-reader-1...[color=blue]
    > hello everybody
    > I've got the following problem. I'm writing a form and I'm using[/color]
    javascript[color=blue]
    > to validate fields (i.e. numeric, not blank). When my scripts detects an
    > error I want it to put the cursor in the corresponding field.
    > I've tried the focus option, but that doesn't seem to work.
    > This is the code I'm using:
    >
    > if (TestOk=="N")
    > {
    > alert('Veld mag niet leeg zijn');
    > veld.select();
    > veld.focus();
    > }
    >
    > The veld.select() does select the error string, the focus, however doesm't
    > seem to work.
    > Anybody got a suggestion?
    > Thanks anyway
    > JW
    >
    >[/color]


    Comment

    • JW

      #3
      Re: Directing the cursor to a input field

      I've tried that format, but it didn't give me any results. I don't get any
      error message, the cursor just doesn't go where I want it to.
      JW

      [color=blue][color=green]
      > > The veld.select() does select the error string, the focus, however[/color][/color]
      doesm't[color=blue][color=green]
      > > seem to work.[/color]
      >
      > Try document.Formna me.Fieldname.fo cus();
      >
      > If that doesn't work (it should though),what error message do actually[/color]
      get?[color=blue]
      >
      > Thx
      > Stu[/color]


      Comment

      • Stuart Palmer

        #4
        Re: Directing the cursor to a input field

        Can you supply your full code? (JS and forms html), what browsers are you
        using too?

        Stu

        "JW" <janwouter@hotp op.com> wrote in message
        news:pLBBc.4645 $%27.145901@zon net-reader-1...[color=blue]
        > I've tried that format, but it didn't give me any results. I don't get[/color]
        any[color=blue]
        > error message, the cursor just doesn't go where I want it to.
        > JW
        >
        >[color=green][color=darkred]
        > > > The veld.select() does select the error string, the focus, however[/color][/color]
        > doesm't[color=green][color=darkred]
        > > > seem to work.[/color]
        > >
        > > Try document.Formna me.Fieldname.fo cus();
        > >
        > > If that doesn't work (it should though),what error message do actually[/color]
        > get?[color=green]
        > >
        > > Thx
        > > Stu[/color]
        >
        >[/color]


        Comment

        • JW

          #5
          Re: Directing the cursor to a input field

          The script I'm using is like the following one. It has to work in IE. Other
          browsers are not important for now, because it's meant as a field check for
          a business application.
          When you use the buttons to set focus to the appropriate field the function
          used to setfocus works perfectly.
          When the NotEmpty function is triggered by leaving the input field the focus
          stays on the next selected field.
          When you want to see this code working visit :


          <html>
          <head>
          <script lang=javascript >
          function NotEmpty(veld)
          {
          TestOk="N";
          invoer=veld.val ue.toString();
          for(var i=0;i<invoer.le ngth;i++)
          {
          var teken =invoer.charAt( i);
          if (teken != ' ')
          {
          TestOk="J";
          i=invoer.length ;
          }
          }
          if (TestOk=="N")
          {
          /* alert('Veld mag niet leeg zijn');*/
          veld.value="Fie ld may not be empty !!";
          focusonfield(ve ld);
          }
          } /*einde functie NotEmpty*/

          function focusonfield(fi eld)
          {

          if (field.name=="i np1")
          {document.form1 .inp1.focus()}

          if (field.name=="i np2")
          {document.form1 .inp2.focus()}

          if (field.name=="i np3")
          {document.form1 .inp3.focus()}

          }
          </script>
          </head>
          <body>
          <form name ="form1">
          <input name="inp1" value="veld1" onchange="NotEm pty(this)";/>
          <input name="inp2" value="veld2" onchange="NotEm pty(this)";/>
          <input name="inp3" value="veld3" onchange="NotEm pty(this)";/>
          <br/>
          <button name="but1" onclick="focuso nfield(form1.in p1)">veld1</button>
          <button name="but2" onclick="focuso nfield(form1.in p2)">veld2</button>
          <button name="but3" onclick="focuso nfield(form1.in p3)">veld3</button>
          </form>
          </body>

          "Stuart Palmer" <tryandspamme@y oucant.com> schreef in bericht
          news:2jqggdF14a gj8U1@uni-berlin.de...[color=blue]
          > Can you supply your full code? (JS and forms html), what browsers are you
          > using too?
          >
          > Stu[/color]


          Comment

          • Lee

            #6
            Re: Directing the cursor to a input field

            JW said:[color=blue]
            >
            >The script I'm using is like the following one. It has to work in IE. Other
            >browsers are not important for now, because it's meant as a field check for
            >a business application.
            >When you use the buttons to set focus to the appropriate field the function
            >used to setfocus works perfectly.
            >When the NotEmpty function is triggered by leaving the input field the focus
            >stays on the next selected field.[/color]

            The problem seems to be that moving focus to the next field occurs
            after the onChange event has completed and it can't be cancelled.

            A hack to work around this is to let the focus move to the next
            field, but then bring it back to where you want it by scheduling
            focus to return after a short delay:

            function focusonfield(fi eld)
            {
            var fieldRef="docum ent.forms['" + field.form.name
            +"'].elements['" + field.name+"']";
            setTimeout(fiel dRef+".select() ;"+fieldRef+".f ocus()",10);
            }

            Comment

            • JW

              #7
              Re: Directing the cursor to a input field

              Makes sense. I'm gonna try it today and let you know the results.
              Thanks
              JW
              [color=blue]
              > The problem seems to be that moving focus to the next field occurs
              > after the onChange event has completed and it can't be cancelled.[/color]


              Comment

              • JW

                #8
                Re: Directing the cursor to a input field

                It worked great! Thanks for your help.
                JW
                [color=blue]
                > Makes sense. I'm gonna try it today and let you know the results.
                > Thanks
                > JW
                >[color=green]
                > > The problem seems to be that moving focus to the next field occurs
                > > after the onChange event has completed and it can't be cancelled.[/color]
                >
                >[/color]


                Comment

                Working...