tags:

views:

52

answers:

1

Hi,

This works :


alert(document.getElementById("Container").nodeName);

But this doesnt :


var CurParent = document.getElementById("Container");
alert(CurParent.nodeName);

I am using IE7. Why ?

+2  A: 

From your latest comment, this seems to be an issue with variable scoping. Are you sure that the var parent is really global? The following will not work, due to improper variable scope:

function firstThing() {
    var parent = document.body;
}

function secondThing() {
    return parent;
}

firstThing();
secondThing(); // will return undefined

Define a variable in the largest scope where you intend to use it. The following will work.

var parent;

function firstThing() {
    parent = document.body;
}

function secondThing() {
    return parent;
}

firstThing();
secondThing(); // will return document.body
Matchu
you are right. but var parent = document.body; // doesnt, if declared and initialized globally.
pokrate
Not sure what that comment means. I'm pretty sure that, if you do a `var parent = whatever` declaration in a different scope, like inside a function, it still has no effect on the larger-scoped variable, because you used the `var` keyword.
Matchu