Events & Event Listeners

Event Reference

What is an event?

Every interaction a user has with a web page; whether that be a click, hovering over something, or simply loading the page.

.addEventListener

Attaches to an element and listens for an event on that element. Takes two arguments. The event to listen for, and the callback that should fire when the event occurs.

Example:

element.addEventListener('event', cb);

The Event handler & object

What is the event handler?

The callback within the event listener. It takes a single argument known as the event object.

Example:

element.addEventListener('click', (event) => {
	// Handle the event here
});

Event Object