User events
Clicks, input, key presses and pointer movement.
Events are signals from the browser: a click happened, a key was pressed, a form was submitted or a page finished loading.
button.addEventListener("click", () => {
status.textContent = "Clicked";
});
Events & Forms
An event is a notification that something happened. User actions, browser lifecycle changes and network activity can all produce events.
JavaScript reacts to events by registering event listeners. A listener is a function that runs when the event is fired.
Good event code keeps behavior predictable: listen to the right element, handle the right event and update the interface in a clear way.
Clicks, input, key presses and pointer movement.
load, resize, visibility and navigation-related events.
A function registered to run when an event happens.
The element where the event started.
Examples
button.addEventListener("click", () => {
panel.hidden = !panel.hidden;
});
<button onclick="togglePanel()">Toggle</button> // Harder to organize, test and reuse.
Code patterns
These examples are the event patterns you will reuse constantly: listen, inspect, prevent, delegate, validate and submit.
Run code after a button click.
button.addEventListener("click", () => {
console.log("clicked");
});
React while a field changes.
field.addEventListener("input", () => {
output.textContent = field.value;
});
Handle a form intentionally.
form.addEventListener("submit", event => {
event.preventDefault();
});
Read the pressed key.
window.addEventListener("keydown", event => {
if (event.key === "Escape") closePanel();
});
Rules that matter
Events are user-facing code. Good handlers stay small, respect native behavior and keep visual, semantic and data state aligned.
It keeps behavior in JavaScript and allows multiple listeners.
click, input and submit mean different things.
Move larger behavior into named functions.
Only preventDefault when you replace the default action.
Events should produce clear feedback when users act.
Register listeners once during setup.
Production thinking
Events are how static pages become interactive applications.
Use real controls such as buttons and form fields so keyboard and assistive technology events work naturally.
Production event code should be registered once, scoped to the feature and easy to remove when needed.
Navigation and content should not depend only on custom click handlers when real links would be more crawlable.
Live code lab
The preview runs inside an isolated iframe. The JavaScript is placed inside the HTML editor for now, so every example stays together and remains easy to understand.
Mini assignment
Practice assignment
Try it yourself
Self-check
If you can explain what fires, where it fires and what default behavior remains, you understand the interaction.