Chapter 1 of ?
html 8 min read

HTML Mastery — Chapter 14: HTML Media — Video & Audio

HTML Event Attributes

HTML event attributes add inline JavaScript handlers directly to elements. While modern development prefers addEventListener(), understanding all event attributes is essential for any HTML developer.

Best Practice: Prefer element.addEventListener('click', handler) over inline onclick="..." attributes. Inline handlers mix HTML and JavaScript, making code harder to maintain, test, and secure (CSP blocks inline event handlers by default).

Live Event Demo

Event Log
Waiting for events...
🖱️ Mouse Events
AttributeFires WhenApplies To
onclickMouse button is clicked (pressed and released) on the element.All visible elements
ondblclickMouse button is double-clicked on the element.All visible elements
onmousedownMouse button is pressed (not yet released) over the element.All visible elements
onmouseupMouse button is released over the element.All visible elements
onmousemoveMouse pointer is moved while over the element. Fires rapidly.All visible elements
onmouseenterMouse pointer enters the element's boundaries. Does NOT bubble.All visible elements
onmouseleaveMouse pointer leaves the element's boundaries. Does NOT bubble.All visible elements
onmouseoverMouse pointer enters element or any of its descendants. Bubbles up.All visible elements
onmouseoutMouse pointer leaves element or any of its descendants. Bubbles up.All visible elements
oncontextmenuRight-click (context menu) is triggered. Can be prevented to create custom menus.All visible elements
onwheelMouse wheel is scrolled over the element.All visible elements
⌨️ Keyboard Events
AttributeFires WhenNotes
onkeydownA key is pressed (any key). Fires before character appears.Use event.key for the key name, event.code for the physical key.
onkeyupA key is released.Use for "submit on Enter" patterns.
onkeypressDeprecated. Formerly fired when a key producing a character is pressed.Use onkeydown instead.
📋 Form Events
AttributeFires WhenBest Used On
onsubmitForm is submitted (via button click or Enter key). Return false to prevent submission.<form>
onresetForm's reset button is clicked, restoring all fields to default values.<form>
oninputValue changes with each keystroke or paste. Fires immediately on every change.Inputs, textareas
onchangeValue changes AND element loses focus (for text) or immediately on selection (for checkboxes, selects).All form elements
onfocusElement receives focus (tab or click). Does not bubble.All form elements
onblurElement loses focus. Does not bubble. Good for validation on departure.All form elements
onfocusinElement receives focus. Bubbles (unlike onfocus).All elements
onfocusoutElement loses focus. Bubbles (unlike onblur).All elements
oninvalidInput fails validation when form is submitted (e.g., required field is empty).Form elements
onsearchUser submits a search field (type="search") via Enter or clicking the X to clear.<input type="search">
onselectUser selects text inside the input or textarea.Inputs, textareas
🌐 Window & Document Events (on <body>)
AttributeFires When
onloadPage and all resources (images, scripts) have finished loading. Use on <body> or <img>.
onunloadPage is about to be unloaded (deprecated/unreliable). Prefer onpagehide or the Beacon API.
onbeforeunloadPage is about to close/navigate away. Return a string to show a "leave page?" confirmation dialog.
onresizeBrowser window is resized. Fires rapidly — use debouncing.
onscrollElement is scrolled. Fires rapidly — use throttling or IntersectionObserver instead.
onerrorAn error occurs while loading a resource (image, script) or when a JavaScript error is uncaught.
onhashchangeThe URL hash (#fragment) changes.
onpopstateThe active history entry changes (browser back/forward).
onstoragelocalStorage or sessionStorage changes in another tab/window.
ononline / onofflineBrowser goes online or offline (network status changes).
🖱️ Drag & Drop Events
AttributeFires OnPurpose
ondragstartDrag sourceWhen the user starts dragging an element. Set event.dataTransfer.setData() here.
ondragDrag sourceFires continuously while element is being dragged.
ondragendDrag sourceWhen the drag operation ends (drop or cancel).
ondragenterDrop targetWhen dragged item enters the drop zone. Add visual feedback here.
ondragoverDrop targetFires while dragged over. Must call event.preventDefault() to allow dropping.
ondragleaveDrop targetWhen dragged item leaves the drop zone. Remove visual feedback.
ondropDrop targetWhen the item is dropped. Read data via event.dataTransfer.getData().
🎬 Media Events (Audio & Video)
AttributeFires When
onplayPlayback begins.
onpausePlayback is paused.
onendedMedia reaches its end.
ontimeupdateCurrent playback position changes (fires about 4 times/second during playback).
onvolumechangeVolume or muted state changes.
onseekingUser starts seeking to a new position.
onseekedSeeking operation completes.
onloadedmetadataMetadata (duration, dimensions) has loaded.
oncanplayEnough data loaded to begin playing without buffering pause.
onwaitingPlayback paused while waiting for more data to buffer.
onratechangePlayback speed changes.
onerrorMedia loading fails.
📱 Touch Events
AttributeFires When
ontouchstartA touch point is placed on the surface.
ontouchmoveA touch point moves across the surface.
ontouchendA touch point is removed from the surface.
ontouchcancelTouch event is interrupted (e.g., alert appears).
🖊️ Pointer Events (Unified Mouse + Touch + Pen)
AttributeFires When
onpointerdownAny pointer device (mouse, touch, pen) makes contact.
onpointerupPointer device is released.
onpointermovePointer device moves.
onpointerenterPointer enters element (no bubble).
onpointerleavePointer leaves element (no bubble).
onpointeroverPointer enters element or descendant (bubbles).
onpointeroutPointer leaves element or descendant (bubbles).
onpointercancelPointer event is cancelled (device goes out of range).
ongotpointercaptureElement receives pointer capture.
onlostpointercaptureElement loses pointer capture.

Chapter Summary

  • Prefer addEventListener() over inline event attributes for production code
  • Use oninput for real-time updates; onchange for after-edit validation
  • Use Pointer Events instead of separate Mouse + Touch Events for unified cross-device handling
  • For ondrop to work, you must call event.preventDefault() inside ondragover
  • Throttle/debounce onresize, onscroll, onmousemove — they fire extremely rapidly
Done with this chapter?
Mark it complete to track your progress and unlock your certificate.
Next Up

Learner Reviews

Write a Review
Share your experience to help other learners.
Your Rating *