Every HTML on* event handler attribute grouped by category: window, form, keyboard, mouse, pointer, drag, clipboard, media and more, with live examples.
Event handler attributes run JavaScript when something happens to an element: a click, a key press, a form submission, a video ending. Each one is the event name with on in front: the click event has onclick, the input event has oninput. They are global, so any element accepts them, although many events only ever fire on certain elements.
HTML
<buttononclick="alert('Saved')">Save</button>
Prefer addEventListener() in real projects. Inline on* attributes mix behavior into markup, allow only one handler per event, run code from a string in the global scope, and are blocked by any Content Security Policy that doesn't allow 'unsafe-inline' (which a good CSP never does). The same code written with addEventListener() keeps working under a strict CSP:
The tables below use the attribute name, but every event listed can be used with addEventListener() by dropping the on ('click', 'keydown', 'submit'). Event names are all lowercase.
<buttononclick="log('inline onclick ran')">Inline handler</button><buttonid="modern">addEventListener</button><olid="out"></ol><script>functionlog(message){const li = document.createElement('li');
li.textContent = message;
document.getElementById('out').append(li);}const modern = document.getElementById('modern');
modern.addEventListener('click',()=>log('first listener'));
modern.addEventListener('click',(event)=>log(`second listener, ${event.type} on <${event.target.localName}>`));</script>
Inside an inline handler, this is the element and event is the event object, so onclick="console.log(event.type)" works. Returning false from an inline handler cancels the default action, but calling event.preventDefault() is clearer.
These fire on the window. As attributes, they go on <body>, which forwards them to the window.
Attribute
Fires when
onafterprint
The print dialog has closed or printing has started.
onbeforeprint
Printing is about to start, before the print preview is built.
onbeforeunload
The page is about to be unloaded. Calling preventDefault() asks the browser to show a "Leave site?" confirmation.
onerror
A script error reaches the window (on <body>), or a resource such as an image or script fails to load (on that element).
onhashchange
The #fragment part of the URL changes.
onlanguagechange
The user's preferred languages change.
onload
The page and all its resources (images, styles, frames) have finished loading. On <img>, <script>, <iframe> and <link>, fires when that resource loads.
onmessage
The window receives a message through postMessage().
onmessageerror
A received message can't be deserialized.
onoffline
The browser loses its network connection.
ononline
The browser regains its network connection.
onpagehide
The user navigates away and the page is hidden (it may be kept in the back/forward cache).
onpagereveal
Newer. The page is about to render its first frame after loading or being restored from the back/forward cache.
onpageshow
The page is shown, including when restored from the back/forward cache (event.persisted is true).
onpageswap
Newer. The page is about to be replaced by a navigation; useful for view transitions across documents.
onpopstate
The active history entry changes, for example with the Back button after history.pushState().
onrejectionhandled
A rejected promise gets a handler late, after unhandledrejection already fired.
onresize
The viewport is resized.
onscroll
The document is scrolled (also fires on any scrollable element).
onstorage
localStorage is changed by another tab or window of the same origin.
onunhandledrejection
A promise is rejected and nothing handles it.
onunload
Deprecated. The page is being unloaded. It breaks the back/forward cache; use onpagehide instead.
Some document-level events have no attribute and work only with addEventListener(): DOMContentLoaded (the HTML is parsed) and the document events readystatechange and visibilitychange (use document.onvisibilitychange or a listener, not a <body> attribute).
Pointer events (from the Pointer Events specification) cover mouse, pen and touch with one API, and are usually a better choice than separate mouse and touch handlers.
Attribute
Fires when
onpointerdown
A pointer becomes active: button pressed, finger down, pen touches.
onpointerup
A pointer is no longer active.
onpointermove
A pointer changes position or pressure.
onpointerover / onpointerout
A pointer enters or leaves the element or a child. Bubbles.
onpointerenter / onpointerleave
A pointer enters or leaves the element itself. Doesn't bubble.
onpointercancel
The browser takes over the pointer, for example to start scrolling.
ongotpointercapture / onlostpointercapture
The element gains or loses pointer capture (setPointerCapture()).
<formid="search"><labelfor="q">Search</label><inputid="q"name="q"required><button>Go</button></form><preid="out">Type, press keys, then submit.</pre><script>const out = document.getElementById('out');const field = document.getElementById('q');constshow=(text)=>(out.textContent = text);
field.addEventListener('input',()=>show(`input: value is now "${field.value}"`));
field.addEventListener('change',()=>show(`change: committed "${field.value}"`));
field.addEventListener('keydown',(event)=>{if(event.key ==='Escape'){
field.value ='';show('keydown: Escape cleared the field');}});
field.addEventListener('invalid',()=>show('invalid: the field is required'));
document.getElementById('search').addEventListener('submit',(event)=>{
event.preventDefault();show(`submit: searching for "${newFormData(event.target).get('q')}"`);});</script>
Pointer events and media events
<divid="pad"style="height: 90px;background: #e0f2fe;border-radius: 8px;display: grid;place-items: center;touch-action: none">
Move, press and release here
</div><videoid="clip"src="/samples/media/fractal.mp4"controlsmutedwidth="240"style="margin-top: 8px"></video><preid="out"></pre><script>const out = document.getElementById('out');constlog=(text)=>{
out.textContent =`${text}\n${out.textContent}`.split('\n').slice(0,6).join('\n');};const pad = document.getElementById('pad');
pad.addEventListener('pointerdown',(e)=>log(`pointerdown (${e.pointerType})`));
pad.addEventListener('pointerup',()=>log('pointerup'));
pad.addEventListener('pointermove',(e)=>{
pad.textContent =`x ${Math.round(e.offsetX)}, y ${Math.round(e.offsetY)}`;});const clip = document.getElementById('clip');for(const type of['loadedmetadata','play','pause','seeked','ended']){
clip.addEventListener(type,()=>log(`video: ${type}`));}</script>