I'm trying to make a button that prints the string "Button clicked" on the console when clicked, but I keep getting the error
Uncaught TypeError: Cannot read properties of null (reading 'addEventListener') at course.js:70
.
This is the code for the HTML button:
<button id="go-button">Go</button>
<br>
Click that button
And this the Javascript for it:
function buttonClicked(){
console.log("Button clicked");
}
var btn = document.getElementById("go-button");
btn.addEventListener("click", buttonClicked, true);
I'm following a video course from 2017 and copied the exact code the instructor wrote, but his runs as intended. I thought that maybe it was an outdated method and that was causing the problem, but then looked it up and about three websites showed similar examples. Honestly, I got confused.