+ 7

[Solved]Why addEventListener not working?

https://code.sololearn.com/WiRirgGZtoTG/?ref=app

7th Jan 2021, 4:11 PM
Ansheeta
Ansheeta - avatar
5 Answers
+ 8
You must apply any JS only after the page content is loaded. So you should put it inside window.onload like this. window.onload = () =>{ document.getElementById("myBtn").addEventListener("click", function() { alert("Hello World!"); }); } Read this for reference. https://www.sololearn.com/post/7444/?ref=app
7th Jan 2021, 4:20 PM
Avinesh
Avinesh - avatar
+ 8
Thank you Avinesh sir
7th Jan 2021, 4:26 PM
Ansheeta
Ansheeta - avatar
+ 6
You should use window.onload to make it work https://code.sololearn.com/WiitLCKU3p0l/?ref=app
7th Jan 2021, 4:26 PM
Harsh
Harsh - avatar
7th Jan 2021, 4:28 PM
Ansheeta
Ansheeta - avatar
+ 4
Ansheeta you're welcome. And just Avinesh is fine.
7th Jan 2021, 4:28 PM
Avinesh
Avinesh - avatar