Kevin Powell - CommunityKP-C
Kevin Powell - Community16mo ago
32 replies
snxxwyy

Javascript questions

Hey, i have a few questions about javascript:

1) Why do people not recommend onclick but rather adding an event listener? What are the differences?

2) Given the example in the codepen, the code increments a counter when a button is clicked. Why is it that inside of the event listener, i have to wrap calling incrementCount() in an arrow function, like so: incrementButton.addEventListener("click", () => incrementCount()), rather than incrementButton.addEventListener("click", incrementCount()) for it to work? If i don't the button doesn't do anything and the count variable is automatically upped to 1 upon page load? https://codepen.io/deerCabin/pen/mdNVBGE

Thank you in advance.
Was this page helpful?