JavaScript events
An event is something that happens in a page, such as a click, input, or key press.
What you will learn
- How to listen for an event
- How the event handler changes the page
Minimal example
button.addEventListener('click', () => { message.textContent = 'Clicked'; });When the button is clicked, the function passed to addEventListener runs.
Common mistakes
- Listen for the event type that matches the control.
- Make sure the element exists before adding a listener.
- Use a button for actions instead of making plain text clickable.