nextSibling doesn't work

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

    #1

    nextSibling doesn't work

    I am trying to use nextSibling to move focus to the next input (text
    box).

    Here's the script:
    function CheckFieldLengt h(e, i)
    {
    // don't move automatically forward if a tab or shift-tab was just
    entered
    if (!(e.keyCode == 16 || e.keyCode == 9))
    {
    if(i.value.leng th == 4 )
    {
    if(i.nextSiblin g != null)
    i.nextSibling.f ocus();
    }
    }
    }

    And here's the call
    <input name="text1" size="4" type="text" maxlength="4"
    onkeyup="CheckF ieldLength(even t, this);" />

    After entering 4 chars, I get a Javascript error that says
    object doesn't support this property or method.

    Where am I going wrong? Thanks

  • Lasse Reichstein Nielsen

    #2
    Re: nextSibling doesn't work

    "BillKi" <bitwise55-group@yahoo.com > writes:
    [color=blue]
    > I am trying to use nextSibling to move focus to the next input (text
    > box).[/color]
    ....[color=blue]
    > if(i.nextSiblin g != null)
    > i.nextSibling.f ocus();[/color]
    ....[color=blue]
    > After entering 4 chars, I get a Javascript error that says
    > object doesn't support this property or method.[/color]

    At what line? My bet is that it's the "focus" property that
    is missing. How do you know that the next sibling is an input
    element, and not, e.g., a text node? (which it probably is :)

    So, try something like this instead:
    ---
    do {
    i = i.nextSibling;
    } while (i != null && i.nodeName != "INPUT");
    if (i != null) {
    i.focus();
    }
    ---

    /L
    --
    Lasse Reichstein Nielsen - lrn@hotpop.com
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
    'Faith without judgement merely degrades the spirit divine.'

    Comment

    • BillKi

      #3
      Re: nextSibling doesn't work

      The error occurs on the line where I try to determine if the
      nextSibling is null
      if(i.nextSiblin g != null)

      Let me try your suggestion and see if that helps...

      Thank you

      Comment

      • BillKi

        #4
        Re: nextSibling doesn't work

        Whoops -- your example put me into an endless loop. Had to kill the
        browser to get out of it!!

        Comment

        • RobG

          #5
          Re: nextSibling doesn't work

          BillKi wrote:[color=blue]
          > Whoops -- your example put me into an endless loop. Had to kill the
          > browser to get out of it!!
          >[/color]

          Please quote what you are replying to. If you are using Google groups,
          click the 'Show options' link and then the 'Reply' link below subject.


          Here's a variation on Lasse's loop:

          while ( i.nextSibling && ( 'INPUT' != i.nodeName ) ) {
          i = i.nextSibling;
          }
          if ( i.focus ) i.focus();


          --
          RobG

          Comment

          • Lasse Reichstein Nielsen

            #6
            Re: nextSibling doesn't work

            "BillKi" <bitwise55-group@yahoo.com > writes:

            Please quote what you are responding to. The example was:
            ---
            do {
            i = i.nextSibling;
            } while (i != null && i.nodeName != "INPUT");
            if (i != null) {
            i.focus();
            }
            ---
            [color=blue]
            > Whoops -- your example put me into an endless loop. Had to kill the
            > browser to get out of it!![/color]

            Interesting ...

            For that code to loop endlessly, it would mean that i never becomes
            equal to "null". Since "undefined" is equal to "null", you must have
            an infinite (most likely cyclic) chain of non-null nextSibling
            elements.

            Can we see the page that exhibits this behavior?

            /L
            --
            Lasse Reichstein Nielsen - lrn@hotpop.com
            DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
            'Faith without judgement merely degrades the spirit divine.'

            Comment

            • Lasse Reichstein Nielsen

              #7
              Re: nextSibling doesn't work

              RobG <rgqld@iinet.ne t.auau> writes:
              [color=blue]
              > Here's a variation on Lasse's loop:[/color]

              Almost :)
              [color=blue]
              > while ( i.nextSibling && ( 'INPUT' != i.nodeName ) ) {[/color]

              If i.nodeName == 'INPUT', then the loop body is not executed, and i is
              not changed. The next time through, i is still the same (input)
              element. You need an extra i=i.nextSibling before the while loop.
              [color=blue]
              > i = i.nextSibling;
              > }
              > if ( i.focus ) i.focus();[/color]

              /L
              --
              Lasse Reichstein Nielsen - lrn@hotpop.com
              DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
              'Faith without judgement merely degrades the spirit divine.'

              Comment

              • BillKi

                #8
                Re: nextSibling doesn't work

                Here's the whole page: The error occurs in the CheckFieldLengt h
                function whereever I refer to i.nextSibling. The error message is
                "object doesn't support this property or method"

                <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
                "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">
                <html xmlns="http://www.w3.org/1999/xhtml">
                <head><title>Te sting Sibling Script</title></head>
                <script type="text/javascript">
                <!--
                function CheckFieldLengt h(e, i)
                {
                // don't move automatically forward if a tab or shift-tab was just
                entered
                if (!(e.keyCode == 16 || e.keyCode == 9))
                {
                if(i.value.leng th == 4 )
                {
                if(i.nextSiblin g != null)
                i.nextSibling.f ocus();
                }
                }
                }

                function CheckForEnter(e , f)
                {
                var keycode;
                if (e.keyCode)
                keycode = e.keyCode;
                else
                keycode = e.which;

                if (keycode == 13)
                {
                f.submit();
                }
                }

                // -->
                </script>
                <body>
                <h1>Testing Sibling Script</h1>
                <form action="testsib lings.htm" method="post" name="MainForm" >
                Voucher Number: <input name="text1" size="4" type="text" maxlength="4"
                onkeyup="CheckF ieldLength(even t, this);" />
                <input name="text2" size="4" type="text" maxlength="4"
                onkeyup="CheckF ieldLength(even t, this);" />
                <input name="text3" size="4" type="text" maxlength="4"
                onkeyup="CheckF ieldLength(even t, this);" />
                <input name="text4" size="4" type="text" maxlength="4"
                onkeypress="Che ckForEnter(even t, this.form);" />
                <br /><input name="btnSubmit " type="button" value="Submit" />
                </form>
                </body>
                </html>

                Comment

                • Lasse Reichstein Nielsen

                  #9
                  Re: nextSibling doesn't work

                  "BillKi" <bitwise55-group@yahoo.com > writes:
                  [color=blue]
                  > Here's the whole page: The error occurs in the CheckFieldLengt h
                  > function whereever I refer to i.nextSibling. The error message is
                  > "object doesn't support this property or method"[/color]

                  In FireFox, I get the error messsage:
                  ---
                  Error: i.nextSibling.f ocus is not a function
                  ---
                  Also, I know that Gecko based browsers will have a text node as
                  next sibling for this HTML code, so that error is easily explained.
                  [color=blue]
                  > <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
                  > "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">[/color]

                  Ah, XHTML, then the nodeName is probably in lower case, not
                  upper case as my code suggested.
                  [color=blue]
                  > <html xmlns="http://www.w3.org/1999/xhtml">
                  > <head><title>Te sting Sibling Script</title></head>
                  > <script type="text/javascript">[/color]

                  The script element must be inside either the head or body element
                  in HTML. Just move the </head> after the script tag.
                  [color=blue]
                  > <!--[/color]

                  That HTML comment is not necessary.
                  [color=blue]
                  > function CheckFieldLengt h(e, i)
                  > {
                  > // don't move automatically forward if a tab or shift-tab was just
                  > entered[/color]

                  (Be careful when posting code that your news client doesn't wrap lines.
                  It's a good idea to keep the posted code to 72 chars to avoid that ...
                  or use a client that doesn't automatically wrap lines)
                  [color=blue]
                  > if (!(e.keyCode == 16 || e.keyCode == 9))
                  > {
                  > if(i.value.leng th == 4 )
                  > {[/color]

                  Ok so far ...
                  [color=blue]
                  > if(i.nextSiblin g != null)
                  > i.nextSibling.f ocus();[/color]

                  I suggest changing these two lines to:
                  ---
                  do {
                  i = i.nextSibling;
                  } while (i && !i.tagName ||
                  i.tagName.toLow erCase() != "input");
                  if (i != null) {
                  i.focus();
                  }
                  ---
                  This is slightly longer than the previous example, because it
                  cares about the case of tag names.

                  It works in FireFox and IE.

                  Good luck.
                  /L
                  --
                  Lasse Reichstein Nielsen - lrn@hotpop.com
                  DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
                  'Faith without judgement merely degrades the spirit divine.'

                  Comment

                  • BillKi

                    #10
                    Re: nextSibling doesn't work

                    >>>>> I suggest changing these two lines to:
                    ---
                    do {
                    i = i.nextSibling;
                    } while (i && !i.tagName ||
                    i.tagName.toLow erCase() != "input");
                    if (i != null) {
                    i.focus();
                    }
                    This is slightly longer than the previous example, because it cares
                    about the case of tag names.

                    It works in FireFox and IE. <<<<

                    And it works for me!!

                    Thank you for your help - Bill

                    Comment

                    • Thomas 'PointedEars' Lahn

                      #11
                      Re: nextSibling doesn't work

                      Lasse Reichstein Nielsen wrote:
                      [color=blue]
                      > [...] it's the "focus" property that is missing. How do you know that the
                      > next sibling is an input element, and not, e.g., a text node? (which it
                      > probably is :)
                      >
                      > So, try something like this instead:
                      > ---
                      > do {
                      > i = i.nextSibling;
                      > } while (i != null && i.nodeName != "INPUT");
                      > if (i != null) {
                      > i.focus();
                      > }
                      > ---[/color]

                      Not this way.
                      If you want to call focus(), test for *it*, not anything else:

                      var t;
                      if ((t = typeof i.focus) == "function" || (t == "object" && i.focus))
                      {
                      i.focus();
                      }

                      <http://pointedears.de/scripts/test/whatami>, ยง2.


                      PointedEars

                      Comment

                      • Lasse Reichstein Nielsen

                        #12
                        Re: nextSibling doesn't work

                        Thomas 'PointedEars' Lahn <PointedEars@we b.de> writes:
                        [color=blue]
                        > Lasse Reichstein Nielsen wrote:[/color]
                        ....[color=blue][color=green]
                        >> if (i != null) {
                        >> i.focus();
                        >> }[/color]
                        >
                        > Not this way.
                        > If you want to call focus(), test for *it*, not anything else:[/color]

                        It's not testing for "focus", admitted, but it isn't testing for
                        anything else either, except the existence of a following input
                        element.

                        Since the "focus" method on an input element has existed in all
                        browsers since Netscape 2, as well as being part of the W3C DOM for
                        HTML since version 1, it's not something I would bother to test for.

                        It would be more reasonable to test for the nextSibling and tagName
                        properties (which the next version I posted used instead of nodeName).
                        [color=blue]
                        > var t;
                        > if ((t = typeof i.focus) == "function" || (t == "object" && i.focus))[/color]

                        This would error if "i" is null, and would not otherwise make anything
                        safer. Remember, when we exit the previous loop, i either points to
                        an input element or is null.


                        The original poster allowed himself to assume that all the relevant
                        input elements of his form were siblings. I find that unlikely, and
                        if not, more complicated code will be needed, but this code (or rather,
                        the updated version posted later) solves the stated problem.

                        /L
                        --
                        Lasse Reichstein Nielsen - lrn@hotpop.com
                        DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
                        'Faith without judgement merely degrades the spirit divine.'

                        Comment

                        • Thomas 'PointedEars' Lahn

                          #13
                          Re: nextSibling doesn't work

                          Lasse Reichstein Nielsen wrote:
                          [color=blue]
                          > Thomas 'PointedEars' Lahn <PointedEars@we b.de> writes:
                          >
                          > [...]
                          > Since the "focus" method on an input element has existed in all
                          > browsers since Netscape 2, as well as being part of the W3C DOM for
                          > HTML since version 1, it's not something I would bother to test for.[/color]

                          I read of UAs here that don't support it anyway, so disagreed. I came to
                          recognize that it is best to test for all host features prior to usage
                          rather than taking chances.
                          [color=blue]
                          > It would be more reasonable to test for the nextSibling and tagName
                          > properties (which the next version I posted used instead of nodeName).[/color]

                          ACK
                          [color=blue][color=green]
                          >> var t;
                          >> if ((t = typeof i.focus) == "function" || (t == "object" && i.focus))[/color]
                          >
                          > This would error if "i" is null, and would not otherwise make anything
                          > safer. Remember, when we exit the previous loop, i either points to
                          > an input element or is null.[/color]

                          Yes, should be

                          var t;
                          if (i && ((t = typeof i.focus) == "function"
                          || (t == "object" && i.focus)))

                          PointedEars

                          Comment

                          Working...