Event types
An event is a signal the browser sends when something happens: a click, a key press, a form being submitted, the page finishing loading. Each kind of signal has a name, called its event type, such as click, keydown or submit. You pass that name to addEventListener to run code when it happens. Knowing which type to listen for is half the work of making a page interactive.
The main families of events
| Family | Common types | Fires when |
|---|---|---|
| Mouse | click, dblclick, mousedown, mouseup, mouseover, mouseout, mouseenter, mouseleave, contextmenu | The user uses a mouse or trackpad |
| Pointer | pointerdown, pointermove, pointerup | Mouse, touch or pen input, handled by one API |
| Keyboard | keydown, keyup | A key is pressed or released |
| Form | input, change, submit, reset | Field values change or a form is sent |
| Focus | focus, blur, focusin, focusout | An element gains or loses focus |
| Document and window | DOMContentLoaded, load, resize, scroll, beforeunload | The page loads, resizes, scrolls or is closing |
| Clipboard | copy, cut, paste | Clipboard actions |
Choosing between similar events
Several pairs look alike but behave differently, and picking the wrong one causes subtle bugs.
inputvschange:inputfires on every keystroke in a text field.changefires only when the value is committed, typically when the field loses focus. For live search, useinput. For checkboxes and selects,changefires immediately on selection.mouseovervsmouseenter:mouseoverbubbles and fires again when moving between child elements.mouseenterdoes not bubble and fires once when entering the element.focusvsfocusin:focusandblurdo not bubble, so a listener on a parent form will not hear them.focusinandfocusoutdo bubble, which suits event delegation.DOMContentLoadedvsload: the first fires when the HTML is parsed; the second waits for images, stylesheets and other resources too.
Reading details from the event object
Every listener receives an event object with the details of what happened.
const search = document.querySelector('#search');
const form = document.querySelector('#search-form');
search.addEventListener('input', (e) => {
console.log('Typed so far:', e.target.value);
});
document.addEventListener('keydown', (e) => {
if (e.key === 'Escape') closeModal();
if (e.key === 'k' && (e.ctrlKey || e.metaKey)) {
e.preventDefault(); // stop the browser's own shortcut
search.focus();
}
});
form.addEventListener('submit', (e) => {
e.preventDefault();
runSearch(search.value);
});
window.addEventListener('resize', () => {
console.log('Width is now', window.innerWidth);
});
Mouse events add clientX and clientY for position. Keyboard events provide key (the character, like "a" or "Enter") and code (the physical key, like "KeyA").
Outdated habits to drop
Older tutorials use the keypress event and properties like keyCode or which. These are deprecated. Use keydown with event.key instead. Another habit is listening for click on a submit button instead of submit on the form; that misses users who submit by pressing Enter. Finally, scroll and resize can fire many times per second, so keep their handlers light or throttle them.
Takeaway
Match the event to the intention: input for live typing, change for committed choices, submit for forms, keydown with event.key for shortcuts, and the bubbling focusin/focusout when delegating. The next lesson moves on to the DOM itself.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.