Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript TypeError: result of expression is not an object

Tags:

javascript

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';
}
like image 987
Abhishek Avatar asked Feb 04 '26 02:02

Abhishek


1 Answers

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
};
like image 154
David Tang Avatar answered Feb 06 '26 16:02

David Tang



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!