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.

22nd Sep 2019, 10:33 AM
Noor
Noor - avatar
5 Respuestas
+ 1
// Can you try this code below. It should work: window.onload = function() { document.getElementById("btn").addEventListener("click", myFunction); } function myFunction() { alert("hello"); }
22nd Sep 2019, 10:44 AM
Phurinat Puekkham
Phurinat Puekkham - avatar
0
Could you please show the code
22nd Sep 2019, 10:35 AM
Peter Parkers
Peter Parkers - avatar
0
https://code.sololearn.com/Wbnn57whft69/?ref=app This isn't the actual project code but, I'm getting this same error.
22nd Sep 2019, 10:41 AM
Noor
Noor - avatar
0
Sorry, but it's not working. I'm still getting that error. https://code.sololearn.com/Wbnn57whft69/?ref=app
22nd Sep 2019, 10:52 AM
Noor
Noor - avatar
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")
22nd Sep 2019, 10:57 AM
Phurinat Puekkham
Phurinat Puekkham - avatar