Connecting Tech Pros Worldwide Help | Site Map

Testing for undefined

  #1  
Old July 23rd, 2005, 06:34 PM
Todd Cary
Guest
 
Posts: n/a
If I execute

var d = document.getElementById("menuBar");
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.getElementById("menuBar");
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
  #2  
Old July 23rd, 2005, 06:35 PM
Fred Oz
Guest
 
Posts: n/a

re: Testing for undefined


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

You need to post menuBar's HTML

--
Fred
  #3  
Old July 23rd, 2005, 06:35 PM
Grant Wagner
Guest
 
Posts: n/a

re: Testing for undefined


"Todd Cary" <todd@aristesoftware.com> wrote in message
news:RUCMd.5227$m31.67424@typhoon.sonic.net...[color=blue]
> If I execute
>
> var d = document.getElementById("menuBar");
> 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.getElementById("menuBar");
> 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@agricoreunited.com>
comp.lang.javascript FAQ - http://jibbering.com/faq


  #4  
Old July 23rd, 2005, 06:35 PM
Todd Cary
Guest
 
Posts: n/a

re: Testing for undefined


Grant Wagner wrote:[color=blue]
> "Todd Cary" <todd@aristesoftware.com> wrote in message
> news:RUCMd.5227$m31.67424@typhoon.sonic.net...
>[color=green]
>>If I execute
>>
>> var d = document.getElementById("menuBar");
>> 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.getElementById("menuBar");
>> 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
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Testing for EOF in file with binary data David Warner answers 31 June 15th, 2006 07:35 AM
Testing for undefined properties Michael McDowell answers 4 November 16th, 2005 05:30 AM
testing for non-negativity An answers 7 November 14th, 2005 10:49 AM