Testing for undefined

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

    #1

    Testing for undefined

    If I execute

    var d = document.getEle mentById("menuB ar");
    var c = d.childNodes;
    for (var i=0; i<c.length; i++) {
    alert(c.menu);
    }

    I get 3 strings and 6 undefined's (expected).

    If I execute

    var d = document.getEle mentById("menuB ar");
    var c = d.childNodes;
    for (var i=0; i<c.length; i++) {
    if(c.menu)
    alert(c.menu);
    }

    I get 3 strings and 2 undefined's.

    Confused! I only want the strings....

    Todd
  • Fred Oz

    #2
    Re: Testing for undefined

    Todd Cary wrote:[color=blue]
    > If I execute[/color]
    [...]

    You need to post menuBar's HTML

    --
    Fred

    Comment

    • Grant Wagner

      #3
      Re: Testing for undefined

      "Todd Cary" <todd@aristesof tware.com> wrote in message
      news:RUCMd.5227 $m31.67424@typh oon.sonic.net.. .[color=blue]
      > If I execute
      >
      > var d = document.getEle mentById("menuB ar");
      > var c = d.childNodes;
      > for (var i=0; i<c.length; i++) {
      > alert(c.menu);[/color]

      alert(c[i].menu);
      [color=blue]
      > }
      >
      > I get 3 strings and 6 undefined's (expected).
      >
      > If I execute
      >
      > var d = document.getEle mentById("menuB ar");
      > var c = d.childNodes;
      > for (var i=0; i<c.length; i++) {
      > if(c.menu)[/color]

      if (c[i].menu)
      [color=blue]
      > alert(c.menu);[/color]

      alert(c[i].menu);
      [color=blue]
      > }
      >
      > I get 3 strings and 2 undefined's.
      >
      > Confused! I only want the strings....[/color]

      if ('string' == typeof c[i].menu)

      Some of those childNodes will most likely be empty text nodes, which are
      unlikely to have your -menu- property.

      --
      Grant Wagner <gwagner@agrico reunited.com>
      comp.lang.javas cript FAQ - http://jibbering.com/faq


      Comment

      • Todd Cary

        #4
        Re: Testing for undefined

        Grant Wagner wrote:[color=blue]
        > "Todd Cary" <todd@aristesof tware.com> wrote in message
        > news:RUCMd.5227 $m31.67424@typh oon.sonic.net.. .
        >[color=green]
        >>If I execute
        >>
        >> var d = document.getEle mentById("menuB ar");
        >> var c = d.childNodes;
        >> for (var i=0; i<c.length; i++) {
        >> alert(c.menu);[/color]
        >
        >
        > alert(c[i].menu);
        >
        >[color=green]
        >> }
        >>
        >>I get 3 strings and 6 undefined's (expected).
        >>
        >>If I execute
        >>
        >> var d = document.getEle mentById("menuB ar");
        >> var c = d.childNodes;
        >> for (var i=0; i<c.length; i++) {
        >> if(c.menu)[/color]
        >
        >
        > if (c[i].menu)
        >
        >[color=green]
        >> alert(c.menu);[/color]
        >
        >
        > alert(c[i].menu);
        >
        >[color=green]
        >> }
        >>
        >>I get 3 strings and 2 undefined's.
        >>
        >>Confused! I only want the strings....[/color]
        >
        >
        > if ('string' == typeof c[i].menu)
        >
        > Some of those childNodes will most likely be empty text nodes, which are
        > unlikely to have your -menu- property.
        >[/color]

        After reading the responses to my messages and doing some research, I
        need to completely rewite the script and use DOM core 2 standards. With
        that in mind, I will stick to Class and ID properties.

        Todd

        Comment

        Working...