Cannot read properties of null (reading 'addEventListener')

Viewed 25

I'm getting a error on the console when executing this code. Basically I want to send a console log when clicking on a button.

var send = document.getElementById("sendButton");

send.addEventListener("click", function () {
  console.log("text");
});
<button id="sendButton">Submit</button>

How can I resolve this?

1 Answers

Your code is fine, should work. Must be something else.

BTW you can use inline onclick if you prefer : [https://www.freecodecamp.org/news/html-button-onclick-javascript-click-event-tutorial/][1]

Related