W3docs

Global Event Attributes

Global Event Attributes HTML has the ability to let events trigger actions in a browser, like starting a JavaScript when a user clicks on an element.

An event occurs when the browser reacts to a particular action — a user clicking a mouse button, pressing a key, playing a video, submitting a form, resizing the window, and so on. To respond to an event, you attach a handler: a piece of script (usually JavaScript) that runs when the event fires.

HTML lets you attach a handler directly in markup through an event attribute. Every attribute on this page begins with on followed by the event name (onclick, onkeydown, onsubmit, …). All of them were added in HTML5 and accept JavaScript code as their value:

<button onclick="alert('Clicked!')">Click me</button>

These event attributes are global — most can be placed on any element — but each one is meaningful only in the right context (form attributes on form controls, media attributes on <audio>/<video>, and so on).

Warning

Inline on* attributes are discouraged in modern code. They mix behavior into your markup, can't easily attach more than one handler, and are blocked by a strict Content Security Policy. Prefer addEventListener in a separate script, which keeps HTML and JavaScript separate and lets several handlers listen to the same event:

<button id="myBtn">Click me</button>

<script>
  const btn = document.getElementById("myBtn");
  btn.addEventListener("click", () => {
    alert("Clicked!");
  });
</script>

The tables below list the event attributes by category. For a fuller treatment of how events work in the browser, see JavaScript Events and the related HTML Global Attributes.

Window Events Attributes

Window events are triggered for the window object. They are usually placed on the <body> tag, where the browser maps them onto window.

AttributeDescription
onafterprintFires after the document has been printed.
onbeforeprintFires before the document is printed.
onbeforeunloadFires before the document is unloaded (e.g. the user is about to leave the page).
onblurFires when the window loses focus (the user switches to another tab or app).
onerrorFires when an error occurs while loading the document or a resource.
onhashchangeFires when the fragment (the part of the URL after #) changes.
onloadFires when the whole page — images, CSS and scripts — has finished loading.
onmessageFires when the window receives a message (e.g. from postMessage).
onofflineFires when the browser loses its network connection.
ononlineFires when the browser regains its network connection.
onpagehideFires when the user navigates away from the page.
onpageshowFires when the user navigates to the page.
onpopstateFires when the active history entry changes.
onresizeFires when the window is resized.
onstorageFires when a web storage area (localStorage/sessionStorage) is updated.
onunloadFires when the page is unloaded (the document is being removed).
<body onload="document.body.style.background = '#eef'">
  <p>This page changes its background colour once it has loaded.</p>
</body>

Form Events Attributes

An event that can occur within a form is called a form event. Form events happen when a user opens or closes a form, moves between forms, or works with data on a form.

Form event attributes can be applied to all HTML elements, but they are generally used with form controls such as <input>, <select> and <textarea>.

AttributeDescription
onblurFires when an element loses focus (the user tabs or clicks away from a control).
onchangeFires when the value of a control has changed and the control loses focus.
oncontextmenuFires when the context (right-click) menu is opened on the element.
onfocusFires when an element gains focus.
oninputFires immediately every time the value of a control changes.
oninvalidFires when a submitted control fails constraint validation.
onresetFires when a form is reset to its initial values.
onsearchFires when the user searches in an <input type="search"> field.
onselectFires when the user selects text inside an <input> or <textarea>.
onsubmitFires when a form is submitted.
<form onsubmit="alert('Submitting…'); return false;">
  <input type="text" name="name"
         onfocus="this.style.background = '#ffd'"
         onblur="this.style.background = ''"
         placeholder="Your name">
  <button type="submit">Send</button>
</form>

Returning false from onsubmit (or calling event.preventDefault() in JavaScript) stops the form from actually being sent — handy while you test.

Keyboard Events Attributes

The keyboard event attributes can be applied to all HTML elements.

AttributeDescription
onkeydownFires when a key is pressed down. Repeats while the key is held.
onkeypressFires when a character key is pressed. Deprecated — use onkeydown instead.
onkeyupFires when a pressed key is released.
<input type="text"
       onkeyup="document.getElementById('echo').textContent = this.value"
       placeholder="Type here">
<p>You typed: <span id="echo"></span></p>

Mouse Events Attributes

Mouse events occur when the mouse interacts with the HTML document. The attributes can be applied to all HTML elements.

AttributeDescription
onclickFires when the element is clicked.
ondblclickFires when the element is double-clicked.
ondragFires repeatedly while an element is being dragged.
ondragendFires when a drag operation ends.
ondragenterFires when a dragged element enters a valid drop target.
ondragleaveFires when a dragged element leaves a valid drop target.
ondragoverFires repeatedly while a dragged element is over a valid drop target.
ondragstartFires when the user starts dragging an element.
ondropFires when a dragged element is dropped on a valid drop target.
onmousedownFires when a mouse button is pressed over the element.
onmousemoveFires whenever the pointer moves while it is over the element.
onmouseoutFires when the pointer moves out of the element.
onmouseoverFires when the pointer moves onto the element.
onmouseupFires when a mouse button is released over the element.
onmousewheelObsolete — use onwheel instead.
onscrollFires when an element's scrollbar is scrolled.
onwheelFires when the mouse wheel rolls up or down over the element.
<div onmousemove="this.textContent = event.offsetX + ', ' + event.offsetY"
     style="width:240px; height:120px; background:#eef; text-align:center; line-height:120px;">
  Move the mouse here
</div>

Clipboard Events Attributes

AttributeDescription
oncopyFires when the content of an element is copied.
oncutFires when the content of an element is cut.
onpasteFires when content is pasted into an element.

Media Events Attributes

Media events occur in media elements, such as video, image, and audio. The attributes can be applied to any HTML element, but they are generally used within audio, embed, img, object and video elements.

AttributeDescription
onabortFires when loading of the media is aborted.
oncanplayFires when the browser has buffered enough to start playing.
oncanplaythroughFires when the browser estimates it can play through without stopping to buffer.
oncuechangeFires when the active cue of a text track (e.g. subtitles or captions) changes.
ondurationchangeFires when the duration of the media changes.
onemptiedFires when the media becomes empty (e.g. the connection is lost).
onendedFires when playback reaches the end of the media.
onerrorFires when an error occurs while loading the media.
onloadeddataFires when the current frame of media data has loaded.
onloadedmetadataFires when metadata (duration, dimensions, …) has loaded.
onloadstartFires when the browser starts loading the media.
onpauseFires when playback is paused.
onplayFires when playback starts or resumes.
onplayingFires when playback is actually running (after buffering, for example).
onprogressFires periodically while the browser is loading the media.
onratechangeFires when the playback rate changes (e.g. fast-forward).
onseekedFires when a seek operation ends.
onseekingFires when a seek operation starts.
onstalledFires when the browser is trying, but failing, to fetch media data.
onsuspendFires when media loading is intentionally paused.
ontimeupdateFires as the playback position changes during playback.
onvolumechangeFires when the volume changes (including muting).
onwaitingFires when playback stops because the next frame isn't buffered yet.

Other Events

AttributeDescription
ontoggleFires when the user opens or closes a <details> element.
Info

Some attributes you may see in older references — onredo, onundo and onshow — were never part of standard HTML or are not supported by browsers, so they have been left out here.

Practice

Practice
What are global event attributes in HTML?
What are global event attributes in HTML?
Was this page helpful?