Function called with Event Trigger | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Function called with Event Trigger

I'm not sure why the function runs without being called. I only wanted to declare it for the button https://code.sololearn.com/WBiWA9Mtr49T/?ref=app

3rd Jan 2020, 2:20 PM
Joshua Morgan
Joshua Morgan - avatar
2 Answers
+ 4
Joshua Morgan , see this line 3: test.addEventListener('click', doThis()) you shouldn't include parantheses () after function name doThis It should be like this: test.addEventListener('click', doThis); because of parantheses function is called immediately and result `undefined` is returned to addeventlistener your statement becomes test.addEventListener('click', undefined) hence it fails to work~ review the lesson : https://www.sololearn.com/learn/JavaScript/2758/
3rd Jan 2020, 2:29 PM
🇮🇳Omkar🕉
🇮🇳Omkar🕉 - avatar
+ 1
Oh my goodness I should really know better with my experience. Definitely not my first eventListener I promise! 😅
3rd Jan 2020, 3:51 PM
Joshua Morgan
Joshua Morgan - avatar