Lesson 69 +5 XP

HTML Event Reference

HTML Event Reference

Events are things that happen in the browser: a click, a key press, a page loading. Every event can be handled with an on... attribute, or with JavaScript.

Window and document events

AttributeWhen it fires
onloadThe page (or element) finished loading.
onunloadThe page is being unloaded.
onresizeThe window was resized.
onscrollThe page was scrolled.
onerrorAn error occurred.
onhashchangeThe hash in the URL changed.
onpageshow / onpagehideUser navigates to/away from the page.
onpopstateThe history entry changed.
onstorageWeb storage changed.
onbeforeunloadJust before the page closes.
onoffline / ononlineThe internet connection dropped/came back.

Mouse events

AttributeWhen it fires
onclickThe element was clicked.
ondblclickDouble-clicked.
onmousedownA mouse button was pressed.
onmouseupA mouse button was released.
onmousemoveThe mouse moved over the element.
onmouseoverThe mouse entered the element.
onmouseoutThe mouse left the element.
onmouseenter / onmouseleaveEntered/left (no bubbling).
oncontextmenuRight-click (context menu).
onwheelThe mouse wheel moved.

Keyboard events

AttributeWhen it fires
onkeydownA key was pressed down.
onkeyupA key was released.
onkeypressA key was pressed (older; prefer keydown).
oninputAn input value changed (fires on every keystroke).

Form events

AttributeWhen it fires
onsubmitA form was submitted.
onresetA form was reset.
onchangeA field changed and lost focus.
onfocusAn element gained focus.
onblurAn element lost focus.
onselectText was selected in a field.
oninvalidA required field failed validation.
onsearchA search field was searched.

Drag and drop events

AttributeWhen it fires
ondragAn element is being dragged.
ondragstartThe drag just started.
ondragendThe drag ended.
ondragoverThe dragged item is over a drop zone.
ondragenterThe dragged item entered a drop zone.
ondragleaveThe dragged item left a drop zone.
ondropThe item was dropped.

Clipboard events

AttributeWhen it fires
oncopyContent was copied.
oncutContent was cut.
onpasteContent was pasted.

Media events (<audio>, <video>)

AttributeWhen it fires
onplayPlayback started.
onplayingPlayback is actually running.
onpausePlayback was paused.
onendedThe media finished.
oncanplayReady to play.
oncanplaythroughReady to play without buffering.
onwaitingBuffering, waiting for data.
onloadeddataThe current frame loaded.
onloadedmetadataMetadata (length, size) loaded.
onprogressThe browser is downloading.
onstalledTrying to load but no data.
onsuspendDownload paused.
onabortLoading was aborted.
onerrorA media error occurred.
onvolumechangeVolume or muted changed.
onratechangePlayback speed changed.
ontimeupdateThe play position updated.
onseeked / onseekingSeek finished / started.
ondurationchangeThe media length changed.
onemptiedThe media became empty.

Touch events

AttributeWhen it fires
ontouchstartA finger touched the screen.
ontouchmoveA finger moved on the screen.
ontouchendA finger left the screen.
ontouchcancelThe touch was interrupted.

Animation and transition events

AttributeWhen it fires
onanimationstartA CSS animation started.
onanimationendA CSS animation finished.
onanimationiterationA CSS animation looped.
ontransitionendA CSS transition finished.

Using events in JavaScript (the modern way)

Event attributes like onclick are fine for learning. In real projects, add listeners instead:

var btn = document.getElementById("myButton");
btn.addEventListener("click", function () {
  alert("You clicked me!");
});

TL;DR

  • Events fire when things happen: clicks, keys, loads, drags, media playback.
  • Handle them with on... attributes or with addEventListener.
  • onclick, onload, onsubmit, onchange are the big ones you'll use daily.
  • Prefer addEventListener in real projects.