Event Handlers vs Event Listeners in JavaScript
Did you know there is a difference between event handlers and event listeners?
In JavaScript, an event can be e.g. a button click or a mouse hover over. It is important to respond to the events e.g. by showing a modal when a button is clicked.
Today you will learn the two ways to handle events in JavaScript in a nutshell. In addition, I will discuss what is the small difference between the two and which one should you use.
In JavaScript, you can handle events in one of two possible ways:
- Event handlers
- Event listeners
Event Handlers
To use an event handler, use one of the EventHandler
properties of an object. Here is an example of using one called onclick
.
The onclick
event handler is triggered when a user clicks on a button. As a result, "Button clicked."
is output…