HTML5 Event Attributes


Global Event Attributes

HTML 4 added 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 and our DHTML tutorial.
Below are the global event attributes that can be added to HTML5 elements to define event actions.
New : New event attributes in HTML5.

Window Event Attributes

Events triggered for the window object (applies to the <body> tag):
AttributeValueDescription
onafterprintNewscriptScript to be run after the document is printed
onbeforeprintNewscriptScript to be run before the document is printed
onbeforeonloadNewscriptScript to be run before the document loads
onblurscriptScript to be run when the window loses focus
onerrorNewscriptScript to be run when an error occur
onfocusscriptScript to be run when the window gets focus
onhaschangeNewscriptScript to be run when the document has changed
onloadscriptScript to be run when the document loads
onmessageNewscriptScript to be run when the message is triggered
onofflineNewscriptScript to be run when the document goes offline
ononlineNewscriptScript to be run when the document comes online
onpagehideNewscriptScript to be run when the window is hidden
onpageshowNewscriptScript to be run when the window becomes visible
onpopstateNewscriptScript to be run when the window's history changes
onredoNewscriptScript to be run when the document performs a redo
onresizeNewscriptScript to be run when the window is resized
onstorageNewscriptScript to be run when when a Web Storage area is updated
onundoNewscriptScript to be run when the document performs an undo
onunloadNewscriptScript to be run when the user leaves the document


Form Events

Events triggered by actions inside a HTML form (applies to all HTML5 elements, but is most common in form elements):
AttributeValueDescription
onblurscript Script to be run when an element loses focus
onchangescriptScript to be run when an element changes
oncontextmenuNewscript Script to be run when a context menu is triggered
onfocusscript Script to be run when an element gets focus
onformchangeNewscript Script to be run when a form changes
onforminputNewscript Script to be run when a form gets user input
oninputNewscript Script to be run when an element gets user input
oninvalidNewscript Script to be run when an element is invalid
onresetscript Script to be run when a form is reset
Not supported in HTML5
onselectscript Script to be run when an element is selected
onsubmitscriptScript to be run when a form is submitted


Keyboard Events

Events triggered by a keyboard (applies to all HTML5 elements):
AttributeValueDescription
onkeydownscript Script to be run when a key is pressed down
onkeypressscript Script to be run when a key is pressed and released
onkeyupscript Script to be run when a key is released


Mouse Events

Events triggered by a mouse, or similar user actions (applies to all HTML5 elements):
AttributeValueDescription
onclickscript Script to be run on a mouse click
ondblclickscript Script to be run on a mouse double-click
ondragNewscript Script to be run when an element is dragged
ondragendNewscript Script to be run at the end of a drag operation
ondragenterNewscript Script to be run when an element has been dragged to a valid drop target
ondragleaveNewscript Script to be run when an element leaves a valid drop target
ondragoverNewscript Script to be run when an element is being dragged over a valid drop target
ondragstartNewscript Script to be run at the start of a drag operation
ondropNewscript Script to be run when dragged element is being dropped
onmousedownscript Script to be run when a mouse button is pressed
onmousemovescript Script to be run when the mouse pointer moves
onmouseoutscriptScript to be run when the mouse pointer moves out of an element
onmouseoverscriptScript to be run when the mouse pointer moves over an element
onmouseupscript Script to be run when a mouse button is released
onmousewheelNewscript Script to be run when the mouse wheel is being rotated
onscrollNewscript Script to be run when an element's scrollbar is being scrolled


Media Events

Events triggered by medias like videos, images and audio (applies to all HTML5 elements, but is most common in media elements, like audio, embed, img, object, and video):
AttributeValueDescription
onabortscript Script to be run on abort
oncanplayNewscriptScript to be run when a file is ready to start playing (when it has buffered enough to begin)
oncanplaythroughNewscriptScript to be run when a file can be played all the way to the end without pausing for buffering
ondurationchangeNewscript Script to be run when the length of the media changes
onemptiedNewscript Script to be run when something bad happens and the file is suddenly unavailable (like unexpectedly disconnects)
onendedNewscript Script to be run when the media has reach the end (a useful event for messages like "thanks for listening")
onerrorNewscript Script to be run when an error occurs when the file is being loaded
onloadeddataNewscriptScript to be run when media data is loaded
onloadedmetadataNewscriptScript to be run when meta data (like dimensions and duration) are loaded
onloadstartNewscriptScript to be run just as the file begins to load before anything is actually loaded
onpauseNewscript Script to be run when the media is paused either by the user or programmatically
onplayNewscript Script to be run when the media is ready to start playing
onplayingNewscript Script to be run when the media actually has started playing
onprogressNewscript Script to be run when the browser is in the process of getting the media data
onratechangeNewscript Script to be run each time the playback rate changes (like when a user switches to a slow motion or fast forward mode)
onreadystatechangeNewscript Script to be run each time the ready state changes (the ready state tracks the state of the media data)
onseekedNewscript Script to be run when the seeking attribute is set to false indicating that seeking has ended
onseekingNewscript Script to be run when the seeking attribute is set to true indicating that seeking is active
onstalledNewscript Script to be run when the browser is unable to fetch the media data for whatever reason
onsuspendNewscriptScript to be run when fetching the media data is stopped before it is completely loaded for whatever reason
ontimeupdateNewscriptScript to be run when the playing position has changed (like when the user fast forwards to a different point in the media)
onvolumechangeNewscriptScript to be run each time the volume is changed which (includes setting the volume to "mute")
onwaitingNewscriptScript to be run when the media has paused but is expected to resume (like when the media pauses to buffer more data)

Leave a Reply