+ 1
Why am I getting addEventListener error
Please 🥺 someone explain I new to JavaScript https://code.sololearn.com/WY0a5tpGViS0/?ref=app
2 odpowiedzi
+ 5
Use the "onload" event
window.onload = function() { // Code to be executed };
Put your code in the function
+ 4
You are getting the error because your javascript is executed BEFORE the browser managed to render the rest of the webpage. So the button element does not exist yet.
And the solution is to delay the execution of the script until the page is loaded. This is what "window.onload" is for, as Chris said, his suggestion will solve your problem.