click
Device-independent activation for buttons and links.
Mouse, pointer and keyboard events let interfaces respond to different ways people interact.
window.addEventListener("keydown", event => {
if (event.key === "Escape") closePanel();
});
Events & Forms
Mouse events such as click and mouseenter are common, but modern pointer events cover mouse, touch and pen input more broadly.
Keyboard events are essential for shortcuts, dismissing overlays and custom interactions. Use event.key for readable key checks.
Whenever possible, use native HTML controls first. A button already supports mouse, keyboard and accessibility behavior.
Device-independent activation for buttons and links.
Unified pointer input for mouse, touch and pen.
React when a key is pressed.
Readable value such as Escape or Enter.
Examples
<button type="button" data-toggle>Toggle</button>
button.addEventListener("click", togglePanel);
<div data-toggle>Toggle</div> // Now keyboard support, role and focus must be rebuilt.
Code patterns
These examples are the event patterns you will reuse constantly: listen, inspect, prevent, delegate, validate and submit.
Common pattern for closing UI.
window.addEventListener("keydown", event => {
if (event.key === "Escape") closePanel();
});
Use key checks carefully.
field.addEventListener("keydown", event => {
if (event.key === "Enter") submitSearch();
});
Pointer events expose coordinates.
panel.addEventListener("pointermove", event => {
panel.style.setProperty("--x", `${event.clientX}px`);
});
Native buttons already handle keyboard activation.
button.addEventListener("click", togglePanel);
Rules that matter
Events are user-facing code. Good handlers stay small, respect native behavior and keep visual, semantic and data state aligned.
Buttons and links provide built-in input support.
It is clearer than keyCode.
Users expect Escape to close temporary UI.
Focus and shortcuts should remain predictable.
They work across more input devices.
Touch and keyboard users may never trigger it.
Production thinking
Good event handling makes the same interface usable with mouse, keyboard, touch and assistive tools.
Keyboard support is not optional for interactive controls.
Production UI should rely on semantic elements first and only add custom keyboard handling where necessary.
Input events do not directly affect SEO, but usable navigation should not depend on hover-only scripts.
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.