HTML DOM Events
HTML DOM Events
HTML DOM events allow JavaScript to register different event handlers on HTML document elements.
Events are usually combined with functions, and a function is not executed until the event occurs (such as a user clicking a button).
Tip: The event model was standardized in W3C Level 2 DOM Events.
HTML DOM Events
DOM: Indicates the DOM attribute level used.
Mouse Events
| property | Description | DOM |
|---|---|---|
| onclick | An event handler called when the user clicks an object. | 2 |
| oncontextmenu | Triggered when the user right-clicks the mouse to open the context menu. | |
| ondblclick | An event handler called when the user double-clicks an object. | 2 |
| onmousedown | A mouse button is pressed. | 2 |
| onmouseenter | Triggered when the mouse pointer moves over an element. | 2 |
| onmouseleave | Triggered when the mouse pointer moves out of an element. | 2 |
| onmousemove | The mouse is moved. | 2 |
| onmouseover | The mouse is moved over an element. | 2 |
| onmouseout | The mouse is moved away from an element. | 2 |
| onmouseup | A mouse button is released. | 2 |
Keyboard Events
| property | Description | DOM |
|---|---|---|
| onkeydown | A keyboard key is pressed. | 2 |
| onkeypress | A keyboard key is pressed and released. | 2 |
| onkeyup | A keyboard key is released. | 2 |
Frame/Object Events
| property | Description | DOM |
|---|---|---|
| onabort | Image loading is interrupted. ( | 2 |
| onbeforeunload | This event is triggered when the page is about to be left (refreshed or closed). | 2 |
| onerror | An error occurs while loading a document or image. ( | |
| onhashchange | This event is triggered when the anchor part of the current URL is changed. | |
| onload | A page or an image finishes loading. | 2 |
| onpageshow | This event is triggered when a user visits a page. | |
| onpagehide | This event is triggered when a user leaves the current page to navigate to another page. | |
| onresize | A window or frame is resized. | 2 |
| onscroll | An event that occurs when the document is scrolled. | 2 |
| onunload | The user exits the page. ( and ) | 2 |
Form Events
| property | Description | DOM |
|---|---|---|
| onblur | Triggered when an element loses focus. | 2 |
| onchange | This event is triggered when the content of a form element changes ( , | 2 |
| onfocus | triggered when an element gets focus | 2 |
| onfocusin | triggered when an element is about to get focus | 2 |
| onfocusout | triggered when an element is about to lose focus | 2 |
| oninput | triggered when an element gets user input | 3 |
| onreset | Triggered when a form is reset | 2 |
| onsearch | Triggered when the user enters text into a search field ( ) | |
| onselect | Triggered when the user selects text ( and | 2 |
| onsubmit | Triggered when a form is submitted | 2 |
Clipboard Events
| Attribute | Description | DOM |
|---|---|---|
| oncopy | Triggered when the user copies the content of an element | |
| oncut | Triggered when the user cuts the content of an element | |
| onpaste | Triggered when the user pastes the content of an element |
Print Events
| Attribute | Description | DOM |
|---|---|---|
| onafterprint | Triggered when a page has started printing or the print window has been closed | |
| onbeforeprint | Triggered when the page is about to start printing |
Drag Events
| Event | Description | DOM |
|---|---|---|
| ondrag | Triggered while an element is being dragged | |
| ondragend | Triggered when the user finishes dragging an element | |
| ondragenter | Triggered when a dragged element enters a drop target | |
| ondragleave | Triggered when a dragged element leaves a drop target | |
| ondragover | Triggered when a dragged element is over a drop target | |
| ondragstart | Triggered when the user starts dragging an element | |
| ondrop | Triggered when a dragged element is dropped on a target area |
Multimedia (Media) Events
| Event | Description | DOM |
|---|---|---|
| onabort | The event is triggered when the video/audio (audio/video) aborts loading. | |
| oncanplay | The event is triggered when the user can start playing the video/audio (audio/video). | |
| oncanplaythrough | The event is triggered when the video/audio (audio/video) can be played normally without pausing or buffering. | |
| ondurationchange | The event is triggered when the duration of the video/audio (audio/video) changes. | |
| onemptied | triggered when the current playlist is empty | |
| onended | The event is triggered when the video/audio (audio/video) playback ends. | |
| onerror | The event is triggered when an error occurs during loading of video/audio (audio/video) data. | |
| onloadeddata | The event is triggered when the browser loads the current frame of the video/audio (audio/video). | |
| onloadedmetadata | The event is triggered after the metadata of the specified video/audio (audio/video) is loaded. | |
| onloadstart | The event is triggered when the browser starts seeking the specified video/audio (audio/video). | |
| onpause | The event is triggered when the video/audio (audio/video) is paused. | |
| onplay | The event is triggered when the video/audio (audio/video) starts playing. | |
| onplaying | The event is triggered when the video/audio (audio/video) is paused or is ready to resume playing after buffering. | |
| onprogress | The event is triggered when the browser downloads the specified video/audio (audio/video). | |
| onratechange | The event is triggered when the playback speed of the video/audio (audio/video) changes. | |
| onseeked | The event is triggered after the user repositions the playback position of the video/audio (audio/video). | |
| onseeking | The event is triggered when the user starts repositioning the video/audio (audio/video). | |
| onstalled | The event is triggered when the browser obtains media data but the media data is not available. | |
| onsuspend | The event is triggered when the browser aborts reading media data. | |
| ontimeupdate | The event is triggered when the current playback position changes. | |
| onvolumechange | The event is triggered when the volume changes. | |
| onwaiting | The event is triggered when the video needs buffering in order to play the next frame. |
Animation Events
| Event | Description | DOM |
|---|---|---|
| animationend | Triggered when a CSS animation ends playing | |
| animationiteration | Triggered when a CSS animation repeats playing | |
| animationstart | Triggered when a CSS animation starts playing |
Transition events
| Event | Description | DOM |
|---|---|---|
| transitionend | Triggered when a CSS transition has completed. |
Other Events
| Event | Description | DOM |
|---|---|---|
| onmessage | Triggered when a message is received through or from an object (WebSocket, Web Worker, Event Source, or a child frame or parent window) | |
| onmousewheel | Deprecated. Use onwheel Event Alternatives | |
| ononline | Triggered when the browser starts working online. | |
| onoffline | Triggered when the browser starts working offline. | |
| onpopstate | Triggered when the window's browsing history (history object) changes. | |
| onshow | This event when | |
| onstorage | Triggered when Web Storage (HTML 5 Web Storage) is updated | |
| ontoggle | Triggered when the user opens or closes triggered when an element | |
| onwheel | Triggered when the mouse wheel scrolls up or down over an element | |
| visibilitychange | This event is used to detect whether the visibility state of the current page has changed. |
Event Object
Constants
| Static variable | Description | DOM |
|---|---|---|
| CAPTURING-PHASE | the current event phase is the capturing phase (1) | 1 |
| AT-TARGET | the current event is at the target phase, evaluating the target event (1) | 2 |
| BUBBLING-PHASE | the current event is in the bubbling phase (3) | 3 |
Attribute
| Attribute | Description | DOM |
|---|---|---|
| bubbles | Returns a boolean indicating whether the event is a bubbling event type. | 2 |
| cancelable | Returns a boolean indicating whether the event has a default action that can be cancelled. | 2 |
| currentTarget | Returns the element whose event listener triggered the event. | 2 |
| eventPhase | Returns the current phase of event propagation. | 2 |
| target | Returns the element that triggered this event (the event's target node). | 2 |
| timeStamp | Returns the date and time when the event was generated. | 2 |
| type | Returns the name of the event represented by the current Event object. | 2 |
Method
| Method | Description | DOM |
|---|---|---|
| initEvent() | Initialize the properties of the newly created Event object. | 2 |
| preventDefault() | Tells the browser not to perform the default action associated with the event. | 2 |
| stopPropagation() | The event is no longer dispatched. | 2 |
Target event object
Method
| Method | Description | DOM |
|---|---|---|
| addEventListener() | Allows registering listener events on the target event (IE8 = attachEvent()) | 2 |
| dispatchEvent() | Allows sending events to listeners (IE8 = fireEvent()) | 2 |
| removeEventListener() | Run a listener registered on the event target once (IE8 = detachEvent()) | 2 |
Event Listener Object
Method
| Method | Description | DOM |
|---|---|---|
| handleEvent() | Register any object as an event handler | 2 |
Document Event Object
Method
| Method | Description | DOM |
|---|---|---|
| createEvent() | 2 |
Mouse/Keyboard event object
Attribute
| Attribute | Description | DOM |
|---|---|---|
| altKey | Returns whether "ALT" was pressed when the event was triggered. | 2 |
| button | Returns which mouse button was clicked when the event was triggered. | 2 |
| clientX | Returns the horizontal coordinate of the mouse pointer when the event was triggered. | 2 |
| clientY | Returns the vertical coordinate of the mouse pointer when the event is triggered. | 2 |
| ctrlKey | Returns whether the "CTRL" key was pressed when the event was triggered. | 2 |
| Location | Returns the location of the key on the device. | 3 |
| charCode | Returns the letter code of the key value triggered by the onkeypress event. | 2 |
| key | Returns the identifier of a key when it is pressed. | 3 |
| keyCode | Returns the character code of the key value triggered by the onkeypress event, or the key code of the onkeydown or onkeyup event. | 2 |
| which | Returns the character code of the key value triggered by the onkeypress event, or the key code of the onkeydown or onkeyup event. | 2 |
| metaKey | Returns whether the "meta" key was pressed when the event was triggered. | 2 |
| relatedTarget | Returns the node related to the event's target node. | 2 |
| screenX | Returns the horizontal coordinate of the mouse pointer when an event is triggered. | 2 |
| screenY | Returns the vertical coordinate of the mouse pointer when an event is triggered. | 2 |
| shiftKey | Returns whether the "SHIFT" key was pressed when the event was triggered. | 2 |
Method
| Method | Description | W3C |
|---|---|---|
| initMouseEvent() | Initializes the values of a mouse event object. | 2 |
| initKeyboardEvent() | Initializes the values of a keyboard event object. | 3 |