Thursday, September 19, 2019

javascript - i'm getting an Uncaught TypeError: Cannot set property 'onclick' of null?




i'm getting uncaught type error of:
Uncaught TypeError: Cannot set property 'onclick' of null






dice app









js code:



var button = document.getElementById("button");


button.onclick = function() {// error
var print = dice.roll();
printNumber(print);
};

Answer



You're executing your JavaScript before the elements exist. Either move your code to the end of the page or put it within an onload handler.



Ex:







dice app








No comments:

Post a Comment

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...