javascriptconsoleweb-deployment-project

Uncaught TypeError: Cannot read properties of null (reading 'addEventListener'). What to do?


I'm designing a quiz app, but while console logging for a click event, this statement showed up on the console. Code -


startButton.addEventListener("click", function startGame() {
  console.log("started");
});

what should i do to show the statement on my console???


Solution

  • If you get this error it means that your start button is null. 2 possibilities:

    1. Your selector for startButton is incorrect. So you have to adjust your selector.

    2. Your selector is correct but your button is not in the DOM yet. So you have a problem with timing. Try to add the eventlistener at the end of your code