Connecting Tech Pros Worldwide Help | Site Map

Testing for undefined

Todd Cary
Guest
 
Posts: n/a
#1: Jul 23 '05
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
Fred Oz
Guest
 
Posts: n/a
#2: Jul 23 '05

re: Testing for undefined


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

You need to post menuBar's HTML

--
Fred
Grant Wagner
Guest
 
Posts: n/a
#3: Jul 23 '05

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


Todd Cary
Guest
 
Posts: n/a
#4: Jul 23 '05

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 JavaScript / Ajax / DHTML bytes