Cannot read property 'addEventListener' | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Cannot read property 'addEventListener'

Hello guys, so I made a simple score keeper webpage on my desktop. I can run all the files from my browser, but when i upload the HTML, CSS, and JS files onto Sololearn, i get the error of Cannot read property 'addEventListener'. The files work fine with no error on my desktop and browser. Any idea as to why I am getting the error?

22nd Mar 2019, 8:20 PM
Erick Gonzalez
Erick Gonzalez - avatar
4 Answers
+ 6
In Sololearn the Javascript is loaded before the contents of the body tag so your code is trying to find elements that don't exist yet. Wrap your Javascript like this and it should work: document.addEventListener("DOMContentLoaded", function() { // Put your existing code here }); This event listener for the document makes the JavaScript code wait until all of the body tag's contents have loaded before JavaScript code is executed. EDIT: Also just to let you know there's a semantic error when you start the game. It says "Playing to 7" but you only need 5 to win. Fix this simply by either changing the winning score to start at 7 (instead of 5) or make the HTML display "Playing to 5" when you first start (instead of "Playing to 7"). EDIT: No Problem 👍
22nd Mar 2019, 8:55 PM
LynTon
LynTon - avatar
+ 17
Erick Gonzalez How could anyone in the world help you 🙂. You didnt post a code that we can elaborate ^^
22nd Mar 2019, 8:36 PM
r8w9
r8w9 - avatar
+ 1
Woops! Sorry, forgot to make it public. Just made it public, thanks. Link below https://code.sololearn.com/WzKdCilqyxmc/#js
22nd Mar 2019, 8:50 PM
Erick Gonzalez
Erick Gonzalez - avatar
+ 1
Wow! That was a simple solution. thanks !
22nd Mar 2019, 9:00 PM
Erick Gonzalez
Erick Gonzalez - avatar