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