Skip to content Skip to sidebar Skip to footer

Why Is My Element Null?

Why when I do an alert of the value (see below) it returns null? When an element with that ID exists? // make reference to divs var countdown_timer = document.getElementById('count

Solution 1:

It's possible that the javascript is being executed before the elements on your page are not loaded, thus the selector isn't finding anything. Is your javascript above the <body> tag? Try putting it after </body> and see how that works for you.

Another solution is to do:

window.onload = function () {
//put all your JS here
}

Solution 2:

onload = function() {
 // put you code here
}

Solution 3:

Your call to document.getElementById needs to be after the markup for the div.

<divclass="timer"id="countdown_timer"></div><scripttype="text/javascript">var countdown_timer = document.getElementById("countdown_timer");
 ...
</script>

Alternatively, you could use the window.onload event, which would be the better way to go.

Post a Comment for "Why Is My Element Null?"