change focus using variable

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

    change focus using variable

    I'm trying unsuccessfully to use a variable in a script that changes focus.

    Depending on which link a user follows (navigates via keyboard or
    clicks), I'd like to change the focus to an input name="q" in different
    forms on the page.

    <A onClick="change Focus('basicsea rch')"
    HREF="#basic">b asic site search</A>

    <A onClick="change Focus('advanced search')"
    HREF="#advanced ">advanced site search</A>

    The script I have is:

    function changeFocus(cat egory) {
    document.'(cate gory)'.q.focus( );
    return false;
    }

    It doesn't work. I'm not sure how to insert the parameter into the focus
    statement. What am I doing wrong?

    --
    Brian (remove "invalid" from my address to email me)

  • Mick White

    #2
    Re: change focus using variable

    Brian wrote:
    [color=blue]
    >
    > The script I have is:
    >
    > function changeFocus(cat egory) {
    > document.'(cate gory)'.q.focus( );
    > return false;
    > }
    >
    > It doesn't work. I'm not sure how to insert the parameter into the focus
    > statement. What am I doing wrong?
    >[/color]
    document.forms[category].q.focus()

    But why the anchors?

    Mick

    Comment

    • kaeli

      #3
      Re: change focus using variable

      In article <109npo6eeh8q8e 2@corp.supernew s.com>, usenet3
      @julietremblay. com.invalid enlightened us with...[color=blue]
      > I'm trying unsuccessfully to use a variable in a script that changes focus.
      >[/color]

      I will assume these are form elements and the form is named, say, form1.
      [color=blue]
      >
      > The script I have is:
      >
      > function changeFocus(cat egory) {
      > document.'(cate gory)'.q.focus( );
      > return false;
      > }
      >[/color]

      document.forms['form1'].elements[category].focus();

      --
      --
      ~kaeli~
      A lot of money is tainted - It taint yours and it taint mine.



      Comment

      • Brian

        #4
        Re: change focus using variable

        Mick White wrote:
        [color=blue]
        > document.forms[category].q.focus()[/color]

        I'm now using the following script:

        function changeFocus(cat egory) {
        document.forms[category].q.focus();
        return false;
        }

        and the following html (edited for space):

        <A onClick="change Focus('basicsea rch')"
        HREF="#basic">b asic site search</A>

        <A onClick="change Focus('advanced search')"
        HREF="#advanced ">advanced site search</A>

        <H2 id="basic">basi c site search</H2>

        <FORM METHOD="get" ACTION="/foo/bar.pl" NAME="basicsear ch">
        <INPUT TYPE="text" NAME="q">
        </FORM>

        <H2 id="advanced">a dvanced site search</H2>
        <FORM METHOD="get" ACTION="/foo/bar.pl" NAME="advanceds earch">
        <INPUT TYPE="text" NAME="q">
        </FORM>


        Still no dice. Live test case here:

        < http://www.tsmchughs.com/site/searchn >
        [color=blue]
        > why the anchors?[/color]

        When .js is disabled, the bookmark anchor will move to an <h2> element
        just above the form.

        --
        Brian (remove "invalid" from my address to email me)

        Comment

        • Brian

          #5
          Re: change focus using variable

          kaeli wrote:
          [color=blue]
          > usenet3@julietr emblay.com.invalid enlightened us with...
          >[color=green]
          >>I'm trying unsuccessfully to use a variable in a script that changes focus.[/color]
          >
          > I will assume these are form elements and the form is named, say, form1.[/color]

          It's the form element name that changes; the input that should get focus
          is named "q" in each form, e.g.,

          <FORM METHOD="get" ACTION="/foo/bar.pl" NAME="basicsear ch">
          <INPUT TYPE="text" NAME="q">
          </FORM>
          [color=blue][color=green]
          >>The script I have is:
          >>
          >>function changeFocus(cat egory) {
          >> document.'(cate gory)'.q.focus( );
          >> return false;
          >>}[/color]
          >
          > document.forms['form1'].elements[category].focus();[/color]

          I've tried

          function changeFocus(cat egory) {
          document.forms[category].elements['q'].focus();
          return false;
          }

          No luck.

          --
          Brian (remove "invalid" from my address to email me)

          Comment

          • Michael Winter

            #6
            Re: change focus using variable

            On Fri, 07 May 2004 16:43:56 -0400, Brian
            <usenet3@juliet remblay.com.inv alid> wrote:
            [color=blue]
            > Mick White wrote:
            >[color=green]
            >> document.forms[category].q.focus()[/color]
            >
            > I'm now using the following script:
            >
            > function changeFocus(cat egory) {
            > document.forms[category].q.focus();
            > return false;
            > }
            >
            > and the following html (edited for space):
            >
            > <A onClick="change Focus('basicsea rch')"
            > HREF="#basic">b asic site search</A>
            >
            > <A onClick="change Focus('advanced search')"
            > HREF="#advanced ">advanced site search</A>
            >
            > <H2 id="basic">basi c site search</H2>
            >
            > <FORM METHOD="get" ACTION="/foo/bar.pl" NAME="basicsear ch">
            > <INPUT TYPE="text" NAME="q">
            > </FORM>
            >
            > <H2 id="advanced">a dvanced site search</H2>
            > <FORM METHOD="get" ACTION="/foo/bar.pl" NAME="advanceds earch">
            > <INPUT TYPE="text" NAME="q">
            > </FORM>[/color]

            You need to cancel the navigation for the focus to take effect (it also
            acts very strangely on Mozilla, otherwise). Prefix the onclick attribute
            value with "return " so that the "return false" statement in the
            changeFocus() function passes back to the intrinsic event.

            Mike

            --
            Michael Winter
            M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)

            Comment

            • Brian

              #7
              Re: change focus using variable

              Michael Winter wrote:[color=blue]
              > Brian wrote:
              >[color=green]
              >> function changeFocus(cat egory) {
              >> document.forms[category].q.focus();
              >> return false;
              >> }
              >>
              >> <A onClick="change Focus('basicsea rch')"
              >> HREF="#basic">b asic site search</A>[/color]
              >
              > You need to cancel the navigation for the focus to take effect (it
              > also acts very strangely on Mozilla, otherwise).[/color]

              That's what I was noticing, thought Opera seemed to act similarly.
              [color=blue]
              > Prefix the onclick attribute value with "return " so that the "return
              > false" statement in the changeFocus() function passes back to the
              > intrinsic event.[/color]

              That's it! I didn't know I needed to add "return " to the onclick
              statement as you described. Thanks for your help.

              --
              Brian (remove "invalid" from my address to email me)

              Comment

              Working...