Events in JavaScript | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Events in JavaScript

Hey, guys! So, we can add an onclick function, or other events, like this: button.onclick = ()=>{ }; button.onclick = function(){ }; button.addEventListener("click", function{ }); My question is- what are the advantages and disadvantages of event listeners and onclick functions?

13th Oct 2020, 7:48 PM
Vasilis Karapas
Vasilis Karapas - avatar
7 Answers
+ 6
onclick: you can get a reference to it and you can replace the callback with addEventListener: you can add more callback for same event and you can choose if event is triggered on bubble phase or capturing phase
13th Oct 2020, 7:54 PM
KrOW
KrOW - avatar
+ 4
Ok? What do you mean by bubble phase or capturing phase? I'm sorry, I'm not much of a programmer. I just like to write code, so my programming basics are a bit rusty. :D
13th Oct 2020, 7:58 PM
Vasilis Karapas
Vasilis Karapas - avatar
+ 4
Vasilis Karapas Dont worry, the community is here for this too 😉. Anyway, see here for a good explaination https://javascript.info/bubbling-and-capturing
13th Oct 2020, 8:01 PM
KrOW
KrOW - avatar
+ 3
Thank you! ^_^
13th Oct 2020, 8:06 PM
Vasilis Karapas
Vasilis Karapas - avatar
+ 2
I use both in my codes, so I've never taken the time to think about which of the two options is more effective.
13th Oct 2020, 7:49 PM
Vasilis Karapas
Vasilis Karapas - avatar
+ 2
13th Oct 2020, 8:00 PM
Vasilis Karapas
Vasilis Karapas - avatar
+ 1
13th Oct 2020, 8:07 PM
KrOW
KrOW - avatar