Testing for undefined

T

Todd Cary

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
 
G

Grant Wagner

Todd Cary said:
If I execute

var d = document.getElementById("menuBar");
var c = d.childNodes;
for (var i=0; i<c.length; i++) {
alert(c.menu);
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)


if (c.menu)
alert(c.menu);
alert(c.menu);

}

I get 3 strings and 2 undefined's.

Confused! I only want the strings....


if ('string' == typeof c.menu)

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

Todd Cary

Grant said:
If I execute

var d = document.getElementById("menuBar");
var c = d.childNodes;
for (var i=0; i<c.length; i++) {
alert(c.menu);

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)



if (c.menu)

alert(c.menu);

alert(c.menu);


}

I get 3 strings and 2 undefined's.

Confused! I only want the strings....



if ('string' == typeof c.menu)

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


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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top