Home / Learn HTML / HTML Event Attributes
HTML Event Attributes
Events tell your page that something happened: a click, a key, a mouse move, a form change.
Definition
An event is something that happens on the page: a click, a key press, a form submit, the page finishing loading. HTML lets you attach JavaScript to an event with an event attribute such as onclick. The modern way is addEventListener in a script, which keeps HTML and code apart.
| Group | Examples |
|---|---|
| Mouse | click, dblclick, mouseover, mouseout, contextmenu |
| Keyboard | keydown, keyup |
| Form | submit, change, input, focus, blur, reset |
| Page | load, DOMContentLoaded, resize, scroll |
| Drag | dragstart, dragover, drop |
| Media | play, pause, ended, timeupdate |
Theory in plain words
What it is
Event attributes such as onclick and onchange run JavaScript when something happens to an element.
Why we use it
They connect user actions to code.
How it works
The browser fires an event, then runs the handler. Modern code prefers addEventListener over attributes.
Where it is used
- Buttons and forms
- Keyboard shortcuts
- Page load actions
This keeps JavaScript out of your HTML.
Quick quiz
1. Which event fires on a click?
onclick.
2. Which event fires on every typed change?
oninput fires as you type.
3. Which is the better way to attach events?
It keeps HTML clean.
Common questions
Which event fires on a click?
onclick. onclick.
Which event fires on every typed change?
oninput. oninput fires as you type.
Which is the better way to attach events?
addEventListener. It keeps HTML clean.