Why am I getting addEventListener error | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why am I getting addEventListener error

Please 🥺 someone explain I new to JavaScript https://code.sololearn.com/WY0a5tpGViS0/?ref=app

11th Jul 2023, 1:25 AM
GLC
GLC - avatar
2 Answers
+ 5
Use the "onload" event window.onload = function() { // Code to be executed }; Put your code in the function
11th Jul 2023, 2:19 AM
Chris Coder
Chris Coder - avatar
+ 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.
11th Jul 2023, 2:26 AM
Tibor Santa
Tibor Santa - avatar