HTML Event Attributes
Global Event Attributes
One of the new features of HTML 4 is that HTML events can trigger behavior in the browser, such as launching a piece of JavaScript when the user clicks an HTML element.
If you want to learn more about event attributes, please visit JavaScript Tutorial
The table below provides the standard event attributes that can be inserted into HTML/XHTML elements to define event behavior.
New : New attribute events in HTML5.
Window Event Attributes
This event is triggered by the window (applies to the tag):
| property | Value | Description |
|---|---|---|
| onafterprintNew | script | Run a script after the document is printed |
| onbeforeprintNew | script | Run a script before the document is printed |
| onbeforeonloadNew | script | Run a script before the document is loaded |
| onblur | script | Run a script when the window loses focus |
| onerrorNew | script | Run a script when an error occurs |
| onfocus | script | Run a script when the window gains focus |
| onhashchangeNew | script | Run a script when the document changes |
| onload | script | Run a script when the document loads |
| onmessageNew | script | Run a script when a message is triggered |
| onofflineNew | script | Run a script when the document goes offline |
| ononlineNew | script | Run a script when the document goes online |
| onpagehideNew | script | Run a script when the window is hidden |
| onpageshowNew | script | Run a script when the window becomes visible |
| onpopstateNew | script | Run a script when the window history changes |
| onredoNew | script | Run a script when the document performs a redo operation |
| onresizeNew | script | Run a script when the window is resized |
| onstorageNew | script | Run a script when the Web Storage area is updated (when data in storage changes) |
| onundoNew | script | Run a script when the document performs an undo |
| onunloadNew | script | Run a script when the user leaves the document |
Form Events
Form events are triggered in HTML forms (applies to all HTML elements, but the element must be inside a form):
| property | Value | Description |
|---|---|---|
| onblur | script | Run a script when an element loses focus |
| onchange | script | Run a script when an element changes |
| oncontextmenuNew | script | Run a script when a context menu is triggered |
| onfocus | script | Run a script when an element gains focus |
| onformchangeNew | script | Run a script when a form changes |
| onforminputNew | script | Run a script when a form receives user input |
| oninputNew | script | Run a script when an element receives user input |
| oninvalidNew | script | Run a script when an element is invalid |
| onreset | script | Run a script when a form is reset. Not supported in HTML5. |
| onselect | script | Run a script when an element is selected |
| onsubmit | script | Run a script when a form is submitted |
Keyboard Events
| property | Value | Description |
|---|---|---|
| onkeydown | script | Run a script when a key is pressed |
| onkeypress | script | Run a script when a key is pressed and released |
| onkeyup | script | Run a script when a key is released |
Mouse Events
Events triggered by the mouse, similar to user actions:
| property | Value | Description |
|---|---|---|
| onclick | script | Run a script when the mouse is clicked |
| ondblclick | script | Run a script when the mouse is double-clicked |
| ondragNew | script | Run a script when an element is dragged |
| ondragendNew | script | Run a script when a drag operation ends |
| ondragenterNew | script | Run a script when an element is dragged onto a valid drop target |
| ondragleaveNew | script | Run a script when an element leaves a valid drop target |
| ondragoverNew | script | Run a script when an element is dragged over a valid drop target |
| ondragstartNew | script | Run a script when a drag operation starts |
| ondropNew | script | Run a script while a dragged element is being dropped |
| onmousedown | script | Run a script when a mouse button is pressed |
| onmousemove | script | Run a script when the mouse pointer moves |
| onmouseout | script | Run a script when the mouse pointer moves out of an element |
| onmouseover | script | Run a script when the mouse pointer moves over an element |
| onmouseup | script | Run a script when a mouse button is released |
| onmousewheelNew | script | Run a script when the mouse wheel is scrolled |
| onscrollNew | script | Run a script when the scrollbar of an element is scrolled |
Media Events
This event is triggered by videos, images, or audio, and is mostly used with HTML media elements such as