WHY Event is occurring itself in javascript...... | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

WHY Event is occurring itself in javascript......

it should work when i click on button but it's working automatically...... this is code.... <script> document.getElementById('idofbutton').addEventListener('click', alert('hey'));

9th Apr 2019, 2:27 PM
Aditya
Aditya - avatar
3 Answers
+ 1
this is because alert("hey") gets executed when evaluating the argument. Put it inside a function, as follows: addeventlistener('click',function() {alert("hey")})
9th Apr 2019, 2:49 PM
Bebida Roja
Bebida Roja - avatar
+ 3
ok i will try....
9th Apr 2019, 2:50 PM
Aditya
Aditya - avatar
+ 3
thanks bro 😀
9th Apr 2019, 4:14 PM
Aditya
Aditya - avatar