Thursday, 19 September 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

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print ...