What are Event Handler Attributes?

Event handler attributes are special HTML attributes that execute JavaScript code when a specific event occurs. They start with on followed by the event name (e.g. onclick, onmouseover).

While inline event handlers are generally discouraged in modern development (use addEventListener() in JavaScript instead), understanding them is essential for reading legacy code and quick prototyping.

Example Usage

Inline vs addEventListener
<!-- Inline event handler (not recommended) --> <button onclick="alert('Hello!')">Click Me</button> <!-- Modern approach (recommended) --> <button id="myBtn">Click Me</button> <script> document.getElementById('myBtn') .addEventListener('click', () => alert('Hello!')); </script>
Using addEventListener is preferred because it separates JavaScript logic from HTML structure and supports multiple listeners.

Mouse Events

AttributeDescription
onclickFires when the element is clicked.
ondblclickFires when the element is double-clicked.
onmousedownFires when a mouse button is pressed down on the element.
onmouseupFires when a mouse button is released over the element.
onmouseoverFires when the mouse pointer moves onto the element.
onmouseoutFires when the mouse pointer moves out of the element.
onmouseenterFires when the mouse pointer enters the element. Does not bubble.
onmouseleaveFires when the mouse pointer leaves the element. Does not bubble.
onmousemoveFires when the mouse pointer moves while over the element.
onauxclickFires when a non-primary pointer button is clicked (e.g. middle or right mouse button).
oncontextmenuFires when the context menu is triggered (usually right-click).
onwheelFires when the mouse wheel is rotated over the element.
onmousewheelDeprecatedFires on mouse wheel rotation. Use onwheel instead.

Keyboard Events

AttributeDescription
onkeydownFires when a key is pressed down.
onkeyupFires when a key is released.
onkeypressDeprecatedFires when a key is pressed and released. Use onkeydown instead.

Focus Events

AttributeDescription
onfocusFires when the element receives focus.
onblurFires when the element loses focus.
onfocusinFires when the element is about to receive focus. Bubbles unlike onfocus.
onfocusoutFires when the element is about to lose focus. Bubbles unlike onblur.

Form Events

AttributeDescription
onsubmitFires when a form is submitted.
onresetFires when a form is reset.
onchangeFires when the value of an input, select, or textarea changes and the element loses focus.
oninputFires immediately when the value of an input element changes.
onbeforeinputFires before the value of an input element is modified.
oninvalidFires when a form element fails constraint validation.
onformdataFires when a FormData object is constructed from a form.
onselectFires when text is selected inside an input or textarea.
onselectionchangeFires when the text selection within an input changes.
onselectstartFires when the user starts a new text selection.

Clipboard Events

AttributeDescription
oncopyFires when the user copies content from the element.
oncutFires when the user cuts content from the element.
onpasteFires when the user pastes content into the element.

Drag & Drop Events

AttributeDescription
ondragFires continuously while an element is being dragged.
ondragstartFires when the user starts dragging an element.
ondragendFires when a drag operation ends (mouse released or Escape pressed).
ondragenterFires when a dragged element enters a valid drop target.
ondragleaveFires when a dragged element leaves a valid drop target.
ondragoverFires continuously while a dragged element is over a valid drop target.
ondropFires when a dragged element is dropped on a valid drop target.

Touch Events

AttributeDescription
ontouchstartFires when a touch point is placed on the touch surface.
ontouchendFires when a touch point is removed from the touch surface.
ontouchmoveFires when a touch point is moved along the touch surface.
ontouchcancelFires when a touch point is disrupted (e.g. too many touch points).

Pointer Events

AttributeDescription
onpointerdownFires when a pointer becomes active (mouse press, touch, pen).
onpointerupFires when a pointer is no longer active.
onpointermoveFires when a pointer changes coordinates.
onpointerenterFires when a pointer enters the element boundaries. Does not bubble.
onpointerleaveFires when a pointer leaves the element boundaries. Does not bubble.
onpointeroverFires when a pointer moves into the element. Bubbles.
onpointeroutFires when a pointer moves out of the element. Bubbles.
onpointercancelFires when the browser determines a pointer event is unlikely to fire more events.
ongotpointercaptureFires when the element captures a pointer using setPointerCapture().
onlostpointercaptureFires when a captured pointer is released.
onpointerrawupdateFires for every pointer change that doesn't fire pointermove (high-frequency updates).

Scroll Events

AttributeDescription
onscrollFires when the element is scrolled.
onscrollendFires when scrolling has completed.
onscrollsnapchangeFires when a scroll snap target changes (experimental).
onscrollsnapchangingFires when a scroll snap target is about to change (experimental).

Animation Events

AttributeDescription
onanimationstartFires when a CSS animation starts.
onanimationendFires when a CSS animation ends.
onanimationiterationFires when a CSS animation completes one iteration.
onanimationcancelFires when a CSS animation is cancelled.
ontransitionrunFires when a CSS transition is first created.
ontransitionstartFires when a CSS transition actually starts.
ontransitionendFires when a CSS transition completes.
ontransitioncancelFires when a CSS transition is cancelled.

Media Events

AttributeDescription
onplayFires when media playback begins.
onpauseFires when media playback is paused.
onplayingFires when media starts playing after being paused or buffered.
onendedFires when media playback reaches the end.
oncanplayFires when the browser can start playing the media.
oncanplaythroughFires when the browser estimates it can play through media without buffering.
onloadeddataFires when the first frame of media data has been loaded.
onloadedmetadataFires when media metadata (duration, dimensions) has been loaded.
onloadstartFires when the browser starts loading the media resource.
onprogressFires periodically while the browser is downloading media data.
ondurationchangeFires when the duration attribute of the media changes.
onratechangeFires when the playback rate changes.
onseekedFires when a seek operation completes.
onseekingFires when a seek operation begins.
onstalledFires when the browser is trying to fetch media data but it is unexpectedly not available.
onsuspendFires when media data loading has been suspended.
ontimeupdateFires when the current playback time changes.
onvolumechangeFires when the volume or muted state changes.
onwaitingFires when playback stops because the next frame is not available.
onemptiedFires when the media element is reset to its initial state.
onabortFires when media resource loading is aborted (not due to error).
oncuechangeFires when a text track's active cues change.

Loading Events

AttributeDescription
onloadFires when a resource and its dependencies have finished loading.
onerrorFires when a resource fails to load or a script error occurs.
onresizeFires when the element's size changes.

Miscellaneous Events

AttributeDescription
ontoggleFires when a details element is opened or closed, or a popover toggles.
onbeforetoggleFires before a popover or details element toggles its state.
oncloseFires when a dialog element is closed.
oncancelFires when the user dismisses a dialog (e.g. pressing Escape).
oncommandFires when an element invokes a command on another element.
onslotchangeFires when the content distributed into a slot element changes.
onsecuritypolicyviolationFires when a Content Security Policy (CSP) violation occurs.
onfullscreenchangeFires when the element enters or exits fullscreen mode.
onfullscreenerrorFires when an attempt to enter fullscreen mode fails.
oncontentvisibilityautostatechangeFires when an element with content-visibility: auto starts or stops being rendered.
oncontextlostFires when a canvas rendering context is lost.
oncontextrestoredFires when a canvas rendering context is restored.
Tip: In modern JavaScript, prefer element.addEventListener('event', handler) over inline HTML event attributes for better separation of concerns, easier debugging, and support for multiple event listeners.