How Logic Works

Event Listener

Code that waits for something to happen, then reacts.

Definition

An event listener is a function attached to an element that runs when a specific event occurs — a click, a keypress, a page load, a form submission. It is how JavaScript makes web pages interactive. Without event listeners, clicking a button would do nothing.

Example

button.addEventListener('click', function() { alert('Clicked!'); }) attaches a function to a button that shows an alert every time the user clicks it.

See it in context Learn how Event Listener fits into the bigger picture of how software actually works.

Read the Guide →