[Web] Focus on first visible control

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Paul E Collins

    [Web] Focus on first visible control

    I have a Web form with a number of text boxes. If the user submits the
    page with some text boxes filled in, those then become plain text
    (with accompanying <input type="hidden"> fields for when the page is
    resubmitted) - so the number of text boxes varies.

    I was using this to focus on the first text box:

    <body onLoad="documen t.forms[0].elements[0].focus();">

    But it doesn't work when the first text box has been removed. In
    effect, the browser is setting the focus to the new first control
    (with type="hidden") so the caret is not visible.

    How can I focus on the first *visible* input element?

    P.


  • sunami

    #2
    Re: [Web] Focus on first visible control

    you can test if the controle/element is of type == "text".

    Comment

    • Lee

      #3
      Re: [Web] Focus on first visible control

      Paul E Collins said:[color=blue]
      >
      >I have a Web form with a number of text boxes. If the user submits the
      >page with some text boxes filled in, those then become plain text
      >(with accompanying <input type="hidden"> fields for when the page is
      >resubmitted) - so the number of text boxes varies.
      >
      >I was using this to focus on the first text box:
      >
      ><body onLoad="documen t.forms[0].elements[0].focus();">
      >
      >But it doesn't work when the first text box has been removed. In
      >effect, the browser is setting the focus to the new first control
      >(with type="hidden") so the caret is not visible.
      >
      >How can I focus on the first *visible* input element?[/color]

      Whatever server-side code you're using to create the hidden fields "knows" which
      is the first visible field. Have it modify the onload handler accordingly.

      Comment

      • Paul E Collins

        #4
        Re: [Web] Focus on first visible control

        Oh, I've found a solution myself.

        I'm using the 'type' property to check that an input is 'text' (and
        not, say, 'hidden').

        function focusFirstVisib leInput()
        {
        var d = document.forms[0];
        for (var i = 1; i <= d.elements.leng th; i++)
        {
        var e = d.elements['a' + i];
        if (e.type == 'text')
        {
        e.focus();
        break;
        }
        }
        }

        P.


        Comment

        • Mick White

          #5
          Re: [Web] Focus on first visible control

          Paul E Collins wrote:
          [color=blue]
          > Oh, I've found a solution myself.
          >
          > I'm using the 'type' property to check that an input is 'text' (and
          > not, say, 'hidden').
          >
          > function focusFirstVisib leInput()
          > {
          > var d = document.forms[0];[/color]
          [color=blue]
          > for (var i = 1; i <= d.elements.leng th; i++)[/color]

          I would start from 0(zero).
          Mick

          Comment

          • Lee

            #6
            Re: [Web] Focus on first visible control

            Mick White said:[color=blue]
            >
            >Paul E Collins wrote:
            >[color=green]
            >> Oh, I've found a solution myself.
            >>
            >> I'm using the 'type' property to check that an input is 'text' (and
            >> not, say, 'hidden').
            >>
            >> function focusFirstVisib leInput()
            >> {
            >> var d = document.forms[0];[/color]
            >[color=green]
            >> for (var i = 1; i <= d.elements.leng th; i++)[/color]
            >
            >I would start from 0(zero).[/color]

            and end at the last element, even though you should
            never actually get past it in this application:

            for (var i = 0; i < d.elements.leng th; i++)

            Comment

            Working...