English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

HTML Reference Manual

Complete list of HTML tags

HTML Events

The value of the event handler is one or a series of JavaScript expressions, methods, and function calls separated by semicolons, and enclosed in quotes. When an event occurs, the browser will execute this code.

Global event attributes

HTML 4 One of the new features is that it can enable HTML events to trigger behaviors in the browser, such as starting a JavaScript script when a user clicks on an HTML element.

The following table provides standard event attributes that can be inserted into HTML/In XHTML elements, to define event behavior.

HTML5 : HTML5New event attributes.

Window Event Attributes

The event is triggered by the window (for the <body> tag):

AttributeValueDescription
onafterprintHTML5scriptRun the script after the document is printed
onbeforeprintHTML5scriptRun the script before the document is printed
onbeforeonloadHTML5scriptRun the script before the document is loaded
onblurscriptRun the script when the window loses focus
onerrorHTML5scriptRun the script when an error occurs
onfocusscriptRun the script when the window receives focus
onhaschangeHTML5scriptRun the script when the document changes
onloadscriptRun the script when the document is loaded
onmessageHTML5scriptRun the script when a message is triggered
onofflineHTML5scriptRun the script when the document goes offline
ononlineHTML5scriptRun the script when the document goes online
onpagehideHTML5scriptRun the script when the window is hidden
onpageshowHTML5scriptRun the script when the window is visible
onpopstateHTML5scriptRun the script when the window history changes
onredoHTML5scriptRun the script when the document performs a redo operation
onresizeHTML5scriptRun the script when the window size is adjusted
onstorageHTML5scriptRun the script when the Web Storage area is updated (when data in the storage space changes)
onundoHTML5scriptRun the script when the document performs an undo operation
onunloadHTML5scriptRun the script when the user leaves the document

Form Events (Form Events)

Form events are triggered in HTML forms (applicable to all HTML elements, but the HTML element must be within the form):

AttributeValueDescription
onblurscriptRun the script when an element loses focus
onchangescriptRun the script when an element changes
oncontextmenuHTML5scriptRun the script when the context menu is triggered
onfocusscriptRun the script when an element receives focus
onformchangeHTML5scriptRun the script when the form changes
onforminputHTML5scriptRun the script when the form receives user input
oninputHTML5scriptRun the script when an element receives user input
oninvalidHTML5scriptRun the script when an element is invalid
onresetscriptRun the script when the form is reset. HTML 5 Not supported.
onselectscriptRun the script when an element is selected
onsubmitscriptRun the script when the form is submitted

Keyboard Events (Keyboard Events)

AttributeValueDescription
onkeydownscriptRun the script when the key is pressed
onkeypressscriptRun the script when the key is pressed and released
onkeyupscriptRun the script when the key is released

Mouse Events (Mouse Events)

Triggered by mouse events, similar to user actions:

AttributeValueDescription
onclickscriptWhen clicking the mouse, run the script
ondblclickscriptWhen double-clicking the mouse, run the script
onmousedownscriptThe script runs when the mouse button is pressed
onmousemovescriptThe script runs when the mouse pointer moves
onmouseoutscriptThe script runs when the mouse pointer moves out of the element
onmouseoverscriptThe script runs when the mouse pointer is moved over the element
onmouseupscriptThe script runs when the mouse button is released
onmousewheelHTML5scriptThe script runs when the mouse wheel is scrolled

Drag Events

Triggered by dragging the mouse, similar to user behavior:

AttributeValueDescription
ondragHTML5scriptThe script runs when a draggable element is being dragged
ondragendHTML5scriptThe script runs when the drag operation ends
ondragenterHTML5scriptThe script runs when the element is dropped onto a valid drag-and-drop target
ondragleaveHTML5scriptThe script runs when the element leaves a valid drag-and-drop target
ondragoverHTML5scriptThe script runs when the element is dragged over a valid drag-and-drop target
ondragstartHTML5scriptThe script runs when the drag operation starts
ondropHTML5scriptThe script runs when a draggable element is being dragged and dropped
onscrollHTML5scriptThe script runs when the scrollbar of a scrolling element is scrolled

Media Events

Triggered by video (videos), images (images), or audio (audio), commonly used in HTML media elements like <audio>, <embed>, <img>, <object>, and <video>):

AttributeValueDescription
onabortscriptThe script runs when an abort event occurs
oncanplayHTML5scriptThe script runs when the media can start playing but may need to stop due to buffering
oncanplaythroughHTML5scriptThe script runs when the media can be played to the end without stopping due to buffering
ondurationchangeHTML5scriptThe script runs when the media length changes
onemptiedHTML5scriptThe script runs when the media resource element becomes unexpectedly empty (due to network errors, loading errors, etc.)
onendedHTML5scriptThe script runs when the media has reached the end
onerrorHTML5scriptThe script runs when an error occurs during the loading of the element
onloadeddataHTML5scriptThe script runs when loading media data
onloadedmetadataHTML5scriptThe script runs when the duration of the media element and other media data has been loaded
onloadstartHTML5scriptThe script runs when the browser starts loading media data
onpauseHTML5scriptThe script runs when the media data is paused
onplayHTML5scriptThe script runs when the media data is about to start playing
onplayingHTML5scriptThe script runs when the media data has started playing
onprogressHTML5scriptThe script runs while the browser is fetching media data
onratechangeHTML5scriptScript runs when the playback rate of the media data changes
onreadystatechangeHTML5scriptScript runs when the ready state (ready-state) changes
onseekedHTML5scriptScript runs when the media element's positioning attribute [1No longer true and positioning has ended
onseekingHTML5scriptScript runs when the media element's positioning attribute is true and positioning has started
onstalledHTML5scriptScript runs when there is an error in the process of fetching media data (delayed)
onsuspendHTML5scriptScript runs when the browser is already fetching media data but has stopped before retrieving the entire media file
ontimeupdateHTML5scriptScript runs when the media changes its playback position
onvolumechangeHTML5scriptScript runs when the volume of the media changes or when the volume is set to mute
onwaitingHTML5scriptScript runs when the media has stopped playing but intends to continue playing

Other events

AttributeValueDescription
onshowHTML5scriptTriggered when the <menu> element is displayed in context
ontoggleHTML5scriptTriggered when the user opens or closes the <details> element