Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
182
rated 0 times [  186] [ 4]  / answers: 1 / hits: 15950  / 13 Years ago, fri, april 22, 2011, 12:00:00

This should be real simple... But it ain't... >.<



var x = document.getElementById('x');
function snooze()
{
x.style.height = '10px';
}


Upon execution, the error I get is:



TypeError: Result of expression 'x' [null] is not an object.



Edit: Heads up, it works when I put the var declaration inside the function. I don't understand... :-(



function snooze()
{
var x = document.getElementById('x');
x.style.height = '10px';
}

More From » javascript

 Answers
5

Either:




  1. There is no element on the page with id=x.

  2. The code is running before the document is loaded.



If an element with id=x exists, try:



window.onload = function () {
// your code in here
};

[#92607] Wednesday, April 20, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
keric

Total Points: 572
Total Questions: 93
Total Answers: 97

Location: Cyprus
Member since Mon, Oct 24, 2022
2 Years ago
;