DOM event


DOM Events are a signal that something has occurred, or is occurring, and can be triggered by user interactions or by the browser. Client-side scripting languages like JavaScript, JScript, VBScript, and Java can register various event handlers or listeners on the element nodes inside a DOM tree, such as in HTML, XHTML, XUL, and SVG documents.
Examples of DOM Events:
  • When a user clicks the mouse
  • When a web page has loaded
  • When an image has been loaded
  • When the mouse moves over an element
  • When an input field is changed
  • When an HTML form is submitted
  • When a user presses a key
Historically, like DOM, the event models used by various web browsers had some significant differences which caused compatibility problems. To combat this, the event model was standardized by the World Wide Web Consortium in DOM Level 2.

Events

HTML events

Common events

There is a huge collection of events that can be generated by most element nodes:
Note that the event classification above is not exactly the same as W3C's classification.
CategoryTypeAttributeDescriptionBubblesCancelable
MouseclickonclickFires when the pointing device button is clicked over an element. A click is defined as a mousedown and mouseup over the same screen location. The sequence of these events is:
  • mousedown
  • mouseup
  • click
MousedblclickondblclickFires when the pointing device button is double-clicked over an element
MousemousedownonmousedownFires when the pointing device button is pressed over an element
MousemouseuponmouseupFires when the pointing device button is released over an element
MousemouseoveronmouseoverFires when the pointing device is moved onto an element
MousemousemoveonmousemoveFires when the pointing device is moved while it is over an element
MousemouseoutonmouseoutFires when the pointing device is moved away from an element
MousedragstartondragstartFired on an element when a drag is started.
MousedragondragThis event is fired at the source of the drag, that is, the element where dragstart was fired, during the drag operation.
MousedragenterondragenterFired when the mouse is first moved over an element while a drag is occurring.
MousedragleaveondragleaveThis event is fired when the mouse leaves an element while a drag is occurring.
MousedragoverondragoverThis event is fired as the mouse is moved over an element when a drag is occurring.
MousedropondropThe drop event is fired on the element where the drop occurs at the end of the drag operation.
MousedragendondragendThe source of the drag will receive a dragend event when the drag operation is complete, whether it was successful or not.
KeyboardkeydownonkeydownFires before keypress, when a key on the keyboard is pressed.
KeyboardkeypressonkeypressFires after keydown, when a key on the keyboard is pressed.
KeyboardkeyuponkeyupFires when a key on the keyboard is released
HTML frame/objectloadonloadFires when the user agent finishes loading all content within a document, including window, frames, objects and imagesFor elements, it fires when the target element and all of its content has finished loading
HTML frame/objectunloadonunloadFires when the user agent removes all content from a window or frame
For elements, it fires when the target element or any of its content has been removed
HTML frame/objectabortonabortFires when an object/image is stopped from loading before completely loaded
HTML frame/objecterroronerrorFires when an object/image/frame cannot be loaded properly
HTML frame/objectresizeonresizeFires when a document view is resized
HTML frame/objectscrollonscrollFires when an element or document view is scrolled, except that a scroll event on document must bubble to the window
HTML formselectonselectFires when a user selects some text in a text field, including input and textarea
HTML formchangeonchangeFires when a control loses the input focus and its value has been modified since gaining focus
HTML formsubmitonsubmitFires when a form is submitted
HTML formresetonresetFires when a form is reset
HTML formfocusonfocusFires when an element receives focus either via the pointing device or by tab navigation
HTML formbluronblurFires when an element loses focus either via the pointing device or by tabbing navigation
User interfacefocusinSimilar to HTML focus event, but can be applied to any focusable element
User interfacefocusoutSimilar to HTML blur event, but can be applied to any focusable element
User interfaceDOMActivateSimilar to XUL command event. Fires when an element is activated, for instance, through a mouse click or a keypress.
MutationDOMSubtreeModifiedFires when the subtree is modified
MutationDOMNodeInsertedFires when a node has been added as a child of another node
MutationDOMNodeRemovedFires when a node has been removed from a DOM-tree
MutationDOMNodeRemovedFromDocumentFires when a node is being removed from a document
MutationDOMNodeInsertedIntoDocumentFires when a node is being inserted into a document
MutationDOMAttrModifiedFires when an attribute has been modified
MutationDOMCharacterDataModifiedFires when the character data has been modified
ProgressloadstartProgress has begun.
ProgressprogressIn progress. After loadstart has been dispatched.
ProgresserrorProgression failed. After the last progress has been dispatched, or after loadstart has been dispatched if progress has not been dispatched.
ProgressabortProgression is terminated. After the last progress has been dispatched, or after loadstart has been dispatched if progress has not been dispatched.
ProgressloadProgression is successful. After the last progress has been dispatched, or after loadstart has been dispatched if progress has not been dispatched.
ProgressloadendProgress has stopped. After one of error, abort, or load has been dispatched.

Note that the events whose names start with "DOM" are currently not well supported, and for this and other performance reasons are deprecated by the W3C in DOM Level 3. Mozilla and Opera support DOMAttrModified, DOMNodeInserted, DOMNodeRemoved and DOMCharacterDataModified. Chrome and Safari support these events, except for DOMAttrModified.

Touch events

Web browsers running on touch-enabled devices, such as Apple's iOS and Google's Android, generate additional events.
CategoryTypeAttributeDescriptionBubblesCancelable
TouchtouchstartFires when a finger is placed on the touch surface/screen.
TouchtouchendFires when a finger is removed from the touch surface/screen.
TouchtouchmoveFires when a finger already placed on the screen is moved across the screen.
TouchtouchenterFires when a touch point moves onto the interactive area defined by a DOM element.
TouchtouchleaveFires when a touch point moves off the interactive area defined by a DOM element.
TouchtouchcancelA user agent must dispatch this event type to indicate when a TouchPoint has been disrupted in an implementation-specific manner, such as by moving outside the bounds of the UA window. A user agent may also dispatch this event type when the user places more touch points on the touch surface than the device or implementation is configured to store, in which case the earliest TouchPoint object in the TouchList should be removed.

In the W3C draft recommendation, a TouchEvent delivers a TouchList of Touch locations, the modifier keys that were active, a TouchList of Touch locations within the targeted DOM element, and a TouchList of Touch locations that have changed since the previous TouchEvent.
Apple didn't join this working group, and delayed W3C recommendation of its Touch Events Specification by disclosing patents late in the recommendation process.