i'm getting uncaught type error of: Uncaught TypeError: Cannot set property 'onclick' of null
<!DOCTYPE html>
<html>
<head>
<title>dice app</title>
<script src="widget.js"></script>
</head>
<body>
<button id="button">Roll Dice</button>
</body>
</html>
js code:
var button = document.getElementById("button");
button.onclick = function() {// error
var print = dice.roll();
printNumber(print);
};
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:
<!DOCTYPE html>
<html>
<head>
<title>dice app</title>
</head>
<body>
<button id="button">Roll Dice</button>
<script src="widget.js"></script>
</body>
</html>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With