Tutorials References Exercises VideosMenu
Free WebsiteGet CertifiedPro

HTML 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.

To learn more about programming events, please visit our JavaScript tutorial.

Below are the global event attributes that can be added to HTML elements to define event actions.


Window Event Attributes

Events triggered for the window object (applies to the <body> tag):

AttributeValueDescription
onafterprintscriptScript to be run after the document is printed
onbeforeprintscriptScript to be run before the document is printed
onbeforeunloadscriptScript to be run when the document is about to be unloaded
onerrorscriptScript to be run when an error occurs
onhashchangescriptScript to be run when there has been changes to the anchor part of the a URL
onloadscriptFires after the page is finished loading
onmessagescriptScript to be run when the message is triggered
onofflinescriptScript to be run when the browser starts to work offline
ononlinescriptScript to be run when the browser starts to work online
onpagehidescriptScript to be run when a user navigates away from a page
onpageshowscriptScript to be run when a user navigates to a page
onpopstatescriptScript to be run when the window's history changes
onresizescriptFires when the browser window is resized
onstoragescriptScript to be run when a Web Storage area is updated
onunloadscriptFires once a page has unloaded (or the browser window has been closed)


Form Events

Events triggered by actions inside a HTML form (applies to almost all HTML elements, but is most used in form elements):

AttributeValueDescription
onblurscriptFires the moment that the element loses focus
onchangescriptFires the moment when the value of the element is changed
oncontextmenuscriptScript to be run when a context menu is triggered
onfocusscriptFires the moment when the element gets focus
oninputscriptScript to be run when an element gets user input
oninvalidscriptScript to be run when an element is invalid
onresetscriptFires when the Reset button in a form is clicked
onsearchscriptFires when the user writes something in a search field (for <input="search">)
onselectscriptFires after some text has been selected in an element
onsubmitscriptFires when a form is submitted

Keyboard Events

AttributeValueDescription
onkeydownscriptFires when a user is pressing a key
onkeypressscriptFires when a user presses a key
onkeyupscriptFires when a user releases a key

Mouse Events

AttributeValueDescription
onclickscriptFires on a mouse click on the element
ondblclickscriptFires on a mouse double-click on the element
onmousedownscriptFires when a mouse button is pressed down on an element
onmousemovescriptFires when the mouse pointer is moving while it is over an element
onmouseoutscriptFires when the mouse pointer moves out of an element
onmouseoverscriptFires when the mouse pointer moves over an element
onmouseupscriptFires when a mouse button is released over an element
onmousewheelscriptDeprecated. Use the onwheel attribute instead
onwheelscriptFires when the mouse wheel rolls up or down over an element

Drag Events

AttributeValueDescription
ondragscriptScript to be run when an element is dragged
ondragendscriptScript to be run at the end of a drag operation
ondragenterscriptScript to be run when an element has been dragged to a valid drop target
ondragleavescriptScript to be run when an element leaves a valid drop target
ondragoverscriptScript to be run when an element is being dragged over a valid drop target
ondragstartscriptScript to be run at the start of a drag operation
ondropscriptScript to be run when dragged element is being dropped
onscrollscriptScript to be run when an element's scrollbar is being scrolled

Clipboard Events

AttributeValueDescription
oncopyscriptFires when the user copies the content of an element
oncutscriptFires when the user cuts the content of an element
onpastescriptFires when the user pastes some content in an element

Media Events

Events triggered by medias like videos, images and audio (applies to all HTML elements, but is most common in media elements, like <audio>, <embed>, <img>, <object>, and <video>).

Tip: Look at our HTML Audio and Video DOM Reference for more information.

AttributeValueDescription
onabortscriptScript to be run on abort
oncanplayscriptScript to be run when a file is ready to start playing (when it has buffered enough to begin)
oncanplaythroughscriptScript to be run when a file can be played all the way to the end without pausing for buffering
oncuechangescriptScript to be run when the cue changes in a <track> element
ondurationchangescriptScript to be run when the length of the media changes
onemptiedscriptScript to be run when something bad happens and the file is suddenly unavailable (like unexpectedly disconnects)
onendedscriptScript to be run when the media has reach the end (a useful event for messages like "thanks for listening")
onerrorscriptScript to be run when an error occurs when the file is being loaded
onloadeddatascriptScript to be run when media data is loaded
onloadedmetadatascriptScript to be run when meta data (like dimensions and duration) are loaded
onloadstartscriptScript to be run just as the file begins to load before anything is actually loaded
onpausescriptScript to be run when the media is paused either by the user or programmatically
onplayscriptScript to be run when the media is ready to start playing
onplayingscriptScript to be run when the media actually has started playing
onprogressscriptScript to be run when the browser is in the process of getting the media data
onratechangescriptScript to be run each time the playback rate changes (like when a user switches to a slow motion or fast forward mode)
onseekedscriptScript to be run when the seeking attribute is set to false indicating that seeking has ended
onseekingscriptScript to be run when the seeking attribute is set to true indicating that seeking is active
onstalledscriptScript to be run when the browser is unable to fetch the media data for whatever reason
onsuspendscriptScript to be run when fetching the media data is stopped before it is completely loaded for whatever reason
ontimeupdatescriptScript to be run when the playing position has changed (like when the user fast forwards to a different point in the media)
onvolumechangescriptScript to be run each time the volume is changed which (includes setting the volume to "mute")
onwaitingscriptScript to be run when the media has paused but is expected to resume (like when the media pauses to buffer more data)

Misc Events

AttributeValueDescription
ontogglescriptFires when the user opens or closes the <details> element