Connecting Tech Pros Worldwide Help | Site Map

Testing for undefined

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 23rd, 2005, 05:34 PM
Todd Cary
Guest
 
Posts: n/a
Default Testing for undefined

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, 05:35 PM
Fred Oz
Guest
 
Posts: n/a
Default 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, 05:35 PM
Grant Wagner
Guest
 
Posts: n/a
Default 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, 05:35 PM
Todd Cary
Guest
 
Posts: n/a
Default 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
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.