HTML Event Reference

Events are actions or occurrences detected by the browser, such as a user clicking a button, entering text, submitting a form, pressing a key, or loading a page.

JavaScript can listen for these events and respond when they occur. HTML also provides event handler attributes such as onclick, oninput, and onsubmit, although JavaScript event listeners are generally preferred for separating behavior from HTML markup.

Mouse Events

Mouse events occur when a pointing device interacts with an element, including clicking, double-clicking, and moving the pointer over or away from an element.

Event Handler Description
clickonclickOccurs when an element is clicked.
dblclickondblclickOccurs when an element is double-clicked.
mousedownonmousedownOccurs when a pointing-device button is pressed over an element.
mouseuponmouseupOccurs when a pointing-device button is released over an element.
mousemoveonmousemoveOccurs when the pointer moves over an element.
mouseoveronmouseoverOccurs when the pointer enters an element or one of its descendants.
mouseoutonmouseoutOccurs when the pointer leaves an element or enters one of its descendants.
mouseenteronmouseenterOccurs when the pointer enters an element.
mouseleaveonmouseleaveOccurs when the pointer leaves an element.
contextmenuoncontextmenuOccurs when the user requests a context menu.
wheelonwheelOccurs when a mouse wheel or similar input device is used.

Keyboard Events

Keyboard events occur when a user presses or releases a key while interacting with the webpage.

Event Handler Description
keydownonkeydownOccurs when a key is pressed.
keyuponkeyupOccurs when a pressed key is released.

The older keypress event is deprecated and should not be used for new webpages. Use keydown or keyup instead.

Form & Input Events

Form and input events occur when users enter data, change form controls, submit forms, or reset form values.

Event Handler Description
inputoninputOccurs when the value of an editable control changes through user input.
changeonchangeOccurs when the value of a form control is committed or changed.
submitonsubmitOccurs when a form is submitted.
resetonresetOccurs when a form is reset.
invalidoninvalidOccurs when a form control fails constraint validation.
selectonselectOccurs when text is selected in certain editable controls.

Focus Events

Focus events occur when an element receives or loses keyboard or programmatic focus.

Event Handler Description
focusonfocusOccurs when an element receives focus.
bluronblurOccurs when an element loses focus.
focusinonfocusinOccurs when an element or one of its descendants receives focus.
focusoutonfocusoutOccurs when an element or one of its descendants loses focus.

Clipboard Events

Clipboard events occur when content is copied, cut, or pasted using the clipboard.

Event Handler Description
copyoncopyOccurs when content is copied.
cutoncutOccurs when content is cut.
pasteonpasteOccurs when content is pasted.

Drag & Drop Events

Drag-and-drop events are generated while draggable content is picked up, moved, placed over a potential drop target, or dropped.

Event Handler Description
dragondragOccurs while an element is being dragged.
dragstartondragstartOccurs when a drag operation begins.
dragendondragendOccurs when a drag operation ends.
dragenterondragenterOccurs when dragged content enters a potential drop target.
dragleaveondragleaveOccurs when dragged content leaves a potential drop target.
dragoverondragoverOccurs while dragged content is over a potential drop target.
dropondropOccurs when dragged content is dropped onto a drop target.

Media Events

Audio and video elements generate events as media loads, plays, pauses, seeks, changes volume, and reaches the end of playback.

Event Handler Description
canplayoncanplayOccurs when enough media data is available to begin playback.
durationchangeondurationchangeOccurs when the media duration changes.
endedonendedOccurs when playback reaches the end of the media.
loadeddataonloadeddataOccurs when the current media frame has loaded.
loadedmetadataonloadedmetadataOccurs when media metadata has loaded.
pauseonpauseOccurs when playback is paused.
playonplayOccurs when playback begins or resumes.
playingonplayingOccurs when playback is ready to continue after being paused or delayed.
progressonprogressOccurs while the browser is loading media data.
ratechangeonratechangeOccurs when the playback rate changes.
seekedonseekedOccurs when a seek operation has completed.
seekingonseekingOccurs when a seek operation begins.
timeupdateontimeupdateOccurs when the current playback position changes.
volumechangeonvolumechangeOccurs when the volume or muted state changes.
waitingonwaitingOccurs when playback stops temporarily because more data is needed.

Window & Document Events

Window and document-related events can indicate when resources load, errors occur, the page is resized or scrolled, or other browser-level activity takes place.

Event Handler Description
loadonloadOccurs when a resource has finished loading.
erroronerrorOccurs when an error happens while loading or processing a resource.
resizeonresizeOccurs when the browser viewport is resized.
scrollonscrollOccurs when a document or element is scrolled.
beforeunloadonbeforeunloadOccurs before the document is about to be unloaded.
hashchangeonhashchangeOccurs when the fragment identifier in the URL changes.
onlineononlineOccurs when the browser reports that network access has been restored.
offlineonofflineOccurs when the browser reports that network access has been lost.

Using Event Handlers

An HTML event handler attribute places JavaScript directly in the element. For example, the onclick attribute can respond when a button is activated.

<button type="button" onclick="alert('Hello!')">Click Me</button>

For larger scripts, JavaScript can register an event listener instead. This keeps the HTML markup separate from the behavior of the page.

<button type="button" id="example-button">Click Me</button>

<script>
const button = document.querySelector('#example-button');

button.addEventListener('click', function () {
  alert('Hello!');
});
</script>

Event listeners can also make it easier to attach multiple handlers and manage event behavior without adding JavaScript to individual HTML attributes.