HTML - 06 - DOM Events

HTML DOM Events


HTML events are actions that occur to HTML elements, which JavaScript can respond to. By using event listeners, you can execute JavaScript code in reaction to these events.

Example of an Event Handler:

<button onclick="myFunction()">Click me</button>

Using addEventListener:

button.addEventListener("click", myFunction);

HTML Events

An HTML event can be triggered by various actions, either by the browser itself or by user interactions. Here are some examples:

  • The web page has finished loading.
  • An input field’s value has changed.
  • A button has been clicked.

When these events occur, you might want to execute specific code. JavaScript allows you to run code in response to these detected events.

Event Handler Attributes

HTML supports event handler attributes that allow you to embed JavaScript code directly into HTML elements. For example:

<input type="text" onchange="alert('Input changed!')">

Common HTML Events

Here’s a list of some common HTML events along with their descriptions:

EventDescription
onchangeTriggered when the value of an HTML element changes.
onclickTriggered when the user clicks on an HTML element.
onmouseoverTriggered when the mouse moves over an HTML element.
onmouseoutTriggered when the mouse moves away from an HTML element.
onkeydownTriggered when the user presses a key on the keyboard.
onloadTriggered when the browser has finished loading the page.

EventOccurs WhenBelongs To
abortThe loading of a media is abortedUiEventEvent
afterprintA page has started printingEvent
animationendA CSS animation has completedAnimationEvent
animationiterationA CSS animation is repeatedAnimationEvent
animationstartA CSS animation has startedAnimationEvent
beforeprintA page is about to be printedEvent
beforeunloadBefore a document is about to be unloadedUiEventEvent
blurAn element loses focusFocusEvent
canplayThe browser can start playing a media (has buffered enough to begin)Event
canplaythroughThe browser can play through a media without stopping for bufferingEvent
changeThe content of a form element has changedEvent
clickAn element is clicked onMouseEvent
contextmenuAn element is right-clicked to open a context menuMouseEvent
copyThe content of an element is copiedClipboardEvent
cutThe content of an element is cutClipboardEvent
dblclickAn element is double-clickedMouseEvent
dragAn element is being draggedDragEvent
dragendDragging of an element has endedDragEvent
dragenterA dragged element enters the drop targetDragEvent
dragleaveA dragged element leaves the drop targetDragEvent
dragoverA dragged element is over the drop targetDragEvent
dragstartDragging of an element has startedDragEvent
dropA dragged element is dropped on the targetDragEvent
durationchangeThe duration of a media is changedEvent
endedA media has reach the end (“thanks for listening”)Event
errorAn error has occurred while loading a fileProgressEventUiEventEvent
focusAn element gets focusFocusEvent
focusinAn element is about to get focusFocusEvent
focusoutAn element is about to lose focusFocusEvent
fullscreenchangeAn element is displayed in fullscreen modeEvent
fullscreenerrorAn element can not be displayed in fullscreen modeEvent
hashchangeThere has been changes to the anchor part of a URLHashChangeEvent
inputAn element gets user inputInputEventEvent
invalidAn element is invalidEvent
keydownA key is downKeyboardEvent
keypressA key is pressedKeyboardEvent
keyupA key is releasedKeyboardEvent
loadAn object has loadedUiEventEvent
loadeddataMedia data is loadedEvent
loadedmetadataMeta data (like dimensions and duration) are loadedEvent
loadstartThe browser starts looking for the specified mediaProgressEvent
messageA message is received through the event sourceEvent
mousedownThe mouse button is pressed over an elementMouseEvent
mouseenterThe pointer is moved onto an elementMouseEvent
mouseleaveThe pointer is moved out of an elementMouseEvent
mousemoveThe pointer is moved over an elementMouseEvent
mouseoverThe pointer is moved onto an elementMouseEvent
mouseoutThe pointer is moved out of an elementMouseEvent
mouseupA user releases a mouse button over an elementMouseEvent
mousewheelDeprecated. Use the wheel event insteadWheelEvent
offlineThe browser starts working offlineEvent
onlineThe browser starts working onlineEvent
openA connection with the event source is openedEvent
pagehideUser navigates away from a webpagePageTransitionEvent
pageshowUser navigates to a webpagePageTransitionEvent
pasteSome content is pasted in an elementClipboardEvent
pauseA media is pausedEvent
playThe media has started or is no longer pausedEvent
playingThe media is playing after being paused or bufferedEvent
popstateThe window’s history changesPopStateEvent
progressThe browser is downloading media dataEvent
ratechangeThe playing speed of a media is changedEvent
resizeThe document view is resizedUiEventEvent
resetA form is resetEvent
scrollAn scrollbar is being scrolledUiEventEvent
searchSomething is written in a search fieldEvent
seekedSkipping to a media position is finishedEvent
seekingSkipping to a media position is startedEvent
selectUser selects some textUiEventEvent
showAelement is shown as a context menuEvent
stalledThe browser is trying to get unavailable media dataEvent
storageA Web Storage area is updatedStorageEvent
submitA form is submittedEvent
suspendThe browser is intentionally not getting media dataEvent
timeupdateThe playing position has changed (the user moves to a different point in the media)Event
toggleThe user opens or closes the
element
Event
touchcancelThe touch is interruptedTouchEvent
touchendA finger is removed from a touch screenTouchEvent
touchmoveA finger is dragged across the screenTouchEvent
touchstartA finger is placed on a touch screenTouchEvent
transitionendA CSS transition has completedTransitionEvent
unloadA page has unloadedUiEventEvent
volumechangeThe volume of a media is changed (includes muting)Event
waitingA media is paused but is expected to resume (e.g. buffering)Event
wheelThe mouse wheel rolls up or down over an elementWheelEvent