HTML Event Attributes
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):
AttributeValueDescriptiononafterprintscriptScript to be run after the document is printedonbeforeprintscriptScript to be run before the document is printedonbeforeunloadscriptScript to be run when the document is about to be unloadedonerrorscriptScript to be run when an error occursonhashchangescriptScript to be run when there has been changes to the anchor part of the a URLonloadscriptFires after the page is finished loadingonmessagescriptScript to be run when the message is triggeredonofflinescriptScript to be run when the browser starts to work offlineononlinescriptScript to be run when the browser starts to work onlineonpagehidescriptScript to be run when a user navigates away from a pageonpageshowscriptScript to be run when a user navigates to a pageonpopstatescriptScript to be run when the window's history changesonresizescriptFires when the browser window is resizedonstoragescriptScript to be run when a Web Storage area is updatedonunloadscriptFires once a page has unloaded (or the browser window has been closed)
ADVERTISEMENT
Form Events
Events triggered by actions inside a HTML form (applies to almost all HTML elements, but is most used in form elements):
AttributeValueDescriptiononblurscriptFires the moment that the element loses focusonchangescriptFires the moment when the value of the element is changedoncontextmenuscriptScript to be run when a context menu is triggeredonfocusscriptFires the moment when the element gets focusoninputscriptScript to be run when an element gets user inputoninvalidscriptScript to be run when an element is invalidonresetscriptFires when the Reset button in a form is clickedonsearchscriptFires when the user writes something in a search field (for <input="search">)onselectscriptFires after some text has been selected in an elementonsubmitscriptFires when a form is submitted
Keyboard Events
AttributeValueDescriptiononkeydownscriptFires when a user is pressing a keyonkeypressscriptFires when a user presses a keyonkeyupscriptFires when a user releases a key
Mouse Events
AttributeValueDescriptiononclickscriptFires on a mouse click on the elementondblclickscriptFires on a mouse double-click on the elementonmousedownscriptFires when a mouse button is pressed down on an elementonmousemovescriptFires when the mouse pointer is moving while it is over an elementonmouseoutscriptFires when the mouse pointer moves out of an elementonmouseoverscriptFires when the mouse pointer moves over an elementonmouseupscriptFires when a mouse button is released over an elementonmousewheelscriptDeprecated. Use the onwheel attribute insteadonwheelscriptFires when the mouse wheel rolls up or down over an element
Drag Events
AttributeValueDescriptionondragscriptScript to be run when an element is draggedondragendscriptScript to be run at the end of a drag operationondragenterscriptScript to be run when an element has been dragged to a valid drop targetondragleavescriptScript to be run when an element leaves a valid drop targetondragoverscriptScript to be run when an element is being dragged over a valid drop targetondragstartscriptScript to be run at the start of a drag operationondropscriptScript to be run when dragged element is being droppedonscrollscriptScript to be run when an element's scrollbar is being scrolled
Clipboard Events
AttributeValueDescriptiononcopyscriptFires when the user copies the content of an elementoncutscriptFires when the user cuts the content of an elementonpastescriptFires 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.
AttributeValueDescriptiononabortscriptScript to be run on abortoncanplayscriptScript 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 bufferingoncuechangescriptScript to be run when the cue changes in a <track> elementondurationchangescriptScript to be run when the length of the media changesonemptiedscriptScript 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 loadedonloadeddatascriptScript to be run when media data is loadedonloadedmetadatascriptScript to be run when meta data (like dimensions and duration) are loadedonloadstartscriptScript to be run just as the file begins to load before anything is actually loadedonpausescriptScript to be run when the media is paused either by the user or programmaticallyonplayscriptScript to be run when the media is ready to start playingonplayingscriptScript to be run when the media actually has started playingonprogressscriptScript to be run when the browser is in the process of getting the media dataonratechangescriptScript 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 endedonseekingscriptScript to be run when the seeking attribute is set to true indicating that seeking is activeonstalledscriptScript to be run when the browser is unable to fetch the media data for whatever reasononsuspendscriptScript to be run when fetching the media data is stopped before it is completely loaded for whatever reasonontimeupdatescriptScript 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
AttributeValueDescriptionontogglescriptFires when the user opens or closes the <details> element