0
How can I get rid of this error.
I'm working with a project which has a few buttons with EventListeners. But my buttons aren't working & in the console I'm getting this error, "Cannot read property addEventListeners of null". Although I put my js code at the bottom of the document. Please someone help me.
5 Answers
+ 1
// Can you try this code below. It should work:
window.onload = function() {
document.getElementById("btn").addEventListener("click", myFunction);
}
function myFunction() {
alert("hello");
}
0
Could you please show the code
0
https://code.sololearn.com/Wbnn57whft69/?ref=app
This isn't the actual project code but, I'm getting this same error.
0
Sorry, but it's not working.
I'm still getting that error.
https://code.sololearn.com/Wbnn57whft69/?ref=app
0
The problem is the id is not the same as the element in HTML
So it should be:
document.getElementById("btn")
not
document.getElementById("demo")