HTML Event Attributes HTML Reference



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.

Below are the global event attributes that can be added to HTML 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):

AttributeHTML4HTML5Description
onafterprintScript to be run after the document is printed
onbeforeprintScript to be run before the document is printed
onbeforeunloadScript to be run before the document is unloaded
onerrorScript to be run when an error occur
onhaschangeScript to be run when the document has changed
onloadFires after the page is finished loading
onmessageScript to be run when the message is triggered
onofflineScript to be run when the document goes offline
ononlineScript to be run when the document comes online
onpagehideScript to be run when the window is hidden
onpageshowScript to be run when the window becomes visible
onpopstateScript to be run when the window's history changes
onredoScript to be run when the document performs a redo
onresizeFires when the browser window is resized
onstorageScript to be run when a Web Storage area is updated
onundoScript to be run when the document performs an undo
onunloadFires 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):

AttributeHTML4HTML5Description
onblurFires the moment that the element loses focus
onchangeFires the moment when the value of the element is changed
oncontextmenuScript to be run when a context menu is triggered
onfocusFires the moment when the element gets focus
onformchangeScript to be run when a form changes
onforminputScript to be run when a form gets user input
oninputScript to be run when an element gets user input
oninvalidScript to be run when an element is invalid
onresetFires when the Reset button in a form is clicked
Not supported in HTML5
onselectFires after some text has been selected in an element
onsubmitFires when a form is submitted

Keyboard Events

AttributeHTML4HTML5Description
onkeydownFires when a user is pressing a key
onkeypressFires when a user presses a key
onkeyupFires when a user releases a key

Mouse Events

Events triggered by a mouse, or similar user actions:

AttributeHTML4HTML5Description
onclickFires on a mouse click on the element
ondblclickFires on a mouse double-click on the element
ondragScript to be run when an element is dragged
ondragendScript to be run at the end of a drag operation
ondragenterScript to be run when an element has been dragged to a valid drop target
ondragleaveScript to be run when an element leaves a valid drop target
ondragoverScript to be run when an element is being dragged over a valid drop target
ondragstartScript to be run at the start of a drag operation
ondropScript to be run when dragged element is being dropped
onmousedownFires when a mouse button is pressed down on an element
onmousemoveFires when the mouse pointer moves over an element
onmouseoutFires when the mouse pointer moves out of an element
onmouseoverFires when the mouse pointer moves over an element
onmouseupFires when a mouse button is released over an element
onmousewheelScript to be run when the mouse wheel is being rotated
onscrollScript 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 HTML elements, but is most common in media elements, like <audio>, <embed>, <img>, <object>, and <video>):

AttributeHTML4HTML5Description
onabortScript to be run on abort
oncanplayScript to be run when a file is ready to start playing (when it has buffered enough to begin)
oncanplaythroughScript to be run when a file can be played all the way to the end without pausing for buffering
ondurationchangeScript to be run when the length of the media changes
onemptiedScript to be run when something bad happens and the file is suddenly unavailable (like unexpectedly disconnects)
onendedScript to be run when the media has reach the end (a useful event for messages like "thanks for listening")
onerrorScript to be run when an error occurs when the file is being loaded
onloadeddataScript to be run when media data is loaded
onloadedmetadataScript to be run when meta data (like dimensions and duration) are loaded
onloadstartScript to be run just as the file begins to load before anything is actually loaded
onpauseScript to be run when the media is paused either by the user or programmatically
onplayScript to be run when the media is ready to start playing
onplayingScript to be run when the media actually has started playing
onprogressScript to be run when the browser is in the process of getting the media data
onratechangeScript to be run each time the playback rate changes (like when a user switches to a slow motion or fast forward mode)
onreadystatechangeScript to be run each time the ready state changes (the ready state tracks the state of the media data)
onseekedScript to be run when the seeking attribute is set to false indicating that seeking has ended
onseekingScript to be run when the seeking attribute is set to true indicating that seeking is active
onstalledScript to be run when the browser is unable to fetch the media data for whatever reason
onsuspendScript to be run when fetching the media data is stopped before it is completely loaded for whatever reason
ontimeupdateScript to be run when the playing position has changed (like when the user fast forwards to a different point in the media)
onvolumechangeScript to be run each time the volume is changed which (includes setting the volume to "mute")
onwaitingScript to be run when the media has paused but is expected to resume (like when the media pauses to buffer more data)

Relative articles