Master event handling in JavaScript to create interactive web applications
User Interface Events
load
: Fires when a resource and its dependencies have finished loadingunload
: Fires when a page is being unloadedresize
: Fires when the document view is resizedscroll
: Fires when the document or an element is scrollederror
: Fires when a resource failed to loadMouse Events
click
: Fires when an element is clickeddblclick
: Fires when an element is double-clickedmousedown
: Fires when a mouse button is pressed on an elementmouseup
: Fires when a mouse button is released over an elementmousemove
: Fires when the mouse is moved over an elementmouseover
: Fires when the mouse enters an elementmouseout
: Fires when the mouse leaves an elementmouseenter
: Similar to mouseover but doesn’t bubble (doesn’t trigger on child elements)mouseleave
: Similar to mouseout but doesn’t bubblecontextmenu
: Fires when the right mouse button is clickedKeyboard Events
keydown
: Fires when a key is pressedkeyup
: Fires when a key is releasedkeypress
: Fires when a key that produces a character is pressed (deprecated)Form Events
submit
: Fires when a form is submittedreset
: Fires when a form is resetchange
: Fires when the value of an input element changes and loses focusinput
: Fires when the value of an input element changes (immediately)focus
: Fires when an element receives focusblur
: Fires when an element loses focusselect
: Fires when text is selected in an input fieldTouch Events
touchstart
: Fires when a touch point is placed on the touch surfacetouchend
: Fires when a touch point is removed from the touch surfacetouchmove
: Fires when a touch point is moved along the touch surfacetouchcancel
: Fires when a touch point has been disruptedDrag and Drop Events
dragstart
: Fires when the user starts dragging an elementdrag
: Fires when an element is being draggeddragenter
: Fires when a dragged element enters a valid drop targetdragleave
: Fires when a dragged element leaves a valid drop targetdragover
: Fires when a dragged element is over a valid drop targetdrop
: Fires when a dragged element is dropped on a valid drop targetdragend
: Fires when the drag operation endsMedia Events
play
: Fires when media playback has begunpause
: Fires when media playback is pausedended
: Fires when media playback has reached the endvolumechange
: Fires when the volume has changedtimeupdate
: Fires when the current playback position has changedCustom Events
window
and travels down to the target elementwindow
addEventListener
is omitted or set to false
. Capturing phase handlers are less common but useful in specific scenarios.Property | Description |
---|---|
type | The event type (e.g., “click”, “keydown”) |
target | The element that triggered the event |
currentTarget | The element that the event handler is attached to |
timeStamp | The time when the event was created |
bubbles | Whether the event bubbles up through the DOM |
cancelable | Whether the event can be canceled |
defaultPrevented | Whether preventDefault() was called on the event |
Mouse Event Properties
Property | Description |
---|---|
clientX , clientY | Coordinates relative to the viewport |
pageX , pageY | Coordinates relative to the document (includes scrolling) |
screenX , screenY | Coordinates relative to the screen |
offsetX , offsetY | Coordinates relative to the target element |
button | Which mouse button was pressed (0: left, 1: middle, 2: right) |
buttons | Bitmask of buttons pressed during the event |
altKey , ctrlKey , shiftKey , metaKey | Whether modifier keys were pressed during the event |
Keyboard Event Properties
Property | Description |
---|---|
key | The key value (e.g., “a”, “Enter”, “ArrowUp”) |
code | The physical key code (e.g., “KeyA”, “Enter”, “ArrowUp”) |
keyCode , which | Legacy key codes (deprecated) |
location | The location of the key on the keyboard (0: standard, 1: left, 2: right, 3: numpad) |
repeat | Whether the key is being held down |
altKey , ctrlKey , shiftKey , metaKey | Whether modifier keys were pressed during the event |
Form Event Properties
Property | Description |
---|---|
target.value | The current value of the form element |
target.checked | The checked state of checkboxes and radio buttons |
target.selected | The selected state of option elements |
target.files | FileList object for file inputs |
Touch Event Properties
Property | Description |
---|---|
touches | List of all current touch points on the screen |
targetTouches | List of touch points on the target element |
changedTouches | List of touch points that changed in this event |
touches[0].identifier | Unique identifier for a touch point |
touches[0].clientX , touches[0].clientY | Coordinates of the touch point relative to the viewport |
touches[0].pageX , touches[0].pageY | Coordinates of the touch point relative to the document |
touches[0].screenX , touches[0].screenY | Coordinates of the touch point relative to the screen |
Method | Description |
---|---|
preventDefault() | Prevents the default action associated with the event |
stopPropagation() | Stops the event from bubbling up to parent elements |
stopImmediatePropagation() | Stops the event from bubbling and prevents other handlers on the same element |
preventDefault()
. This allows the browser to start scrolling immediately without waiting for JavaScript execution.
{ passive: true }
, calling preventDefault()
in the event handler will have no effect and will generate a console warning.click
, load
, submit
, etc.
addEventListener
and removeEventListener
:
{ passive: true }
to event listeners for touch and wheel events that don’t call preventDefault() to improve scrolling performance.addEventListener
instead of on-properties (onclick
, etc.) to attach multiple handlers and have more control over event handling.addEventListener
to attach event handlers