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 |
|---|---|---|
click | onclick | Occurs when an element is clicked. |
dblclick | ondblclick | Occurs when an element is double-clicked. |
mousedown | onmousedown | Occurs when a pointing-device button is pressed over an element. |
mouseup | onmouseup | Occurs when a pointing-device button is released over an element. |
mousemove | onmousemove | Occurs when the pointer moves over an element. |
mouseover | onmouseover | Occurs when the pointer enters an element or one of its descendants. |
mouseout | onmouseout | Occurs when the pointer leaves an element or enters one of its descendants. |
mouseenter | onmouseenter | Occurs when the pointer enters an element. |
mouseleave | onmouseleave | Occurs when the pointer leaves an element. |
contextmenu | oncontextmenu | Occurs when the user requests a context menu. |
wheel | onwheel | Occurs 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 |
|---|---|---|
keydown | onkeydown | Occurs when a key is pressed. |
keyup | onkeyup | Occurs 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 |
|---|---|---|
input | oninput | Occurs when the value of an editable control changes through user input. |
change | onchange | Occurs when the value of a form control is committed or changed. |
submit | onsubmit | Occurs when a form is submitted. |
reset | onreset | Occurs when a form is reset. |
invalid | oninvalid | Occurs when a form control fails constraint validation. |
select | onselect | Occurs 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 |
|---|---|---|
focus | onfocus | Occurs when an element receives focus. |
blur | onblur | Occurs when an element loses focus. |
focusin | onfocusin | Occurs when an element or one of its descendants receives focus. |
focusout | onfocusout | Occurs 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 |
|---|---|---|
copy | oncopy | Occurs when content is copied. |
cut | oncut | Occurs when content is cut. |
paste | onpaste | Occurs 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 |
|---|---|---|
drag | ondrag | Occurs while an element is being dragged. |
dragstart | ondragstart | Occurs when a drag operation begins. |
dragend | ondragend | Occurs when a drag operation ends. |
dragenter | ondragenter | Occurs when dragged content enters a potential drop target. |
dragleave | ondragleave | Occurs when dragged content leaves a potential drop target. |
dragover | ondragover | Occurs while dragged content is over a potential drop target. |
drop | ondrop | Occurs 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 |
|---|---|---|
canplay | oncanplay | Occurs when enough media data is available to begin playback. |
durationchange | ondurationchange | Occurs when the media duration changes. |
ended | onended | Occurs when playback reaches the end of the media. |
loadeddata | onloadeddata | Occurs when the current media frame has loaded. |
loadedmetadata | onloadedmetadata | Occurs when media metadata has loaded. |
pause | onpause | Occurs when playback is paused. |
play | onplay | Occurs when playback begins or resumes. |
playing | onplaying | Occurs when playback is ready to continue after being paused or delayed. |
progress | onprogress | Occurs while the browser is loading media data. |
ratechange | onratechange | Occurs when the playback rate changes. |
seeked | onseeked | Occurs when a seek operation has completed. |
seeking | onseeking | Occurs when a seek operation begins. |
timeupdate | ontimeupdate | Occurs when the current playback position changes. |
volumechange | onvolumechange | Occurs when the volume or muted state changes. |
waiting | onwaiting | Occurs 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 |
|---|---|---|
load | onload | Occurs when a resource has finished loading. |
error | onerror | Occurs when an error happens while loading or processing a resource. |
resize | onresize | Occurs when the browser viewport is resized. |
scroll | onscroll | Occurs when a document or element is scrolled. |
beforeunload | onbeforeunload | Occurs before the document is about to be unloaded. |
hashchange | onhashchange | Occurs when the fragment identifier in the URL changes. |
online | ononline | Occurs when the browser reports that network access has been restored. |
offline | onoffline | Occurs 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.
