pokrate
pokrate

Reputation: 3974

Problem with document.getElementById

This works :


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

But this doesnt :


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

I am using IE7. Why ?

Upvotes: 1

Views: 237

Answers (1)

Matchu
Matchu

Reputation: 85784

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

Upvotes: 2

Related Questions