+ 3

[Solved] Javascript Functions to add event on html element

button.onclick = function (){} Or.. button.onclick = () => {} And . button.addEventListener("click", function(){}) What's difference between these three styles. Which one will be more practical and good to use. Drop ur ideas.😀

18th Aug 2020, 3:32 PM
Bibek Oli
Bibek Oli - avatar
2 Answers
Use first one, since you need to do removeEventListener for avoid memory leak. In 2 varian this context will be bad since arrow func has no this context., 3 variant just like lamda function so remove event listener couls not call
18th Aug 2020, 3:56 PM
george - avatar
Please see here for an example how can be it done: https://code.sololearn.com/W2mTAN2s05qW/?ref=app
18th Aug 2020, 4:35 PM
JaScript - avatar