Event

HTML event attributes define how elements react to user interactions or browser actions, triggering JavaScript for events like clicks, key presses, or page loads. This makes web pages more interactive and dynamic.

HTML Events Overview

HTML events are interactions or actions in the browser, like mouse clicks, keyboard inputs, or page loads. They can activate JavaScript functions to handle user input. Examples include onclick, onmouseover, and onchange.

Using Inline Event Handlers in HTML

Inline event handlers in HTML allow you to embed JavaScript within HTML elements through attributes like onclick, onmouseover, etc. This makes it possible to trigger specific actions when events such as clicks or mouse movements occur.

Syntax

index.html
<element event="handler">

Event Types

  • Mouse: click , mouseover
  • Keyboard: keydown , keyup
  • Form: submit , change
  • Window: load , resize

Example

index.html
<button onclick="alert('Button clicked!')">Click me</button>

Explanation

When the button is clicked, the onclick event activates the alert() function, showing a pop-up with the message "Button clicked!"

Window Event Attributes

Events that are activated for the window object (commonly used with the body tag):

AttributeValueDescription
onclickscriptScript to be run after the document is printed
onbeforeprintscriptScript to be run before the document is printed
onbeforeunloadscriptScript to be run when the document is about to be unloaded
onerrorscriptScript to be run when an error occurs
onhashchangescriptScript to be run when there have been changes to the anchor part of a URL
onloadscriptFires after the page is finished loading
onmessagescriptScript to be run when the message is triggered
onofflinescriptScript to be run when the browser starts to work offline
ononlinescriptScript to be run when the browser starts to work online
onpagehidescriptScript to be run when a user navigates away from a page
onpageshowscriptScript to be run when a user navigates to a page
onpopstatescriptScript to be run when the window’s history changes
onresizescriptFires when the browser window is resized
onstoragescriptScript to be run when a Web Storage area is updated
onunloadscriptFires once a page has unloaded (or the browser window has been closed)

Form Events

Events that occur due to interactions within an HTML form (commonly used in form elements but applicable to most HTML elements).

AttributeValueDescription
onblurscriptFires when the element loses focus
onchangescriptFires when the value of the element is changed
oncontextmenuscriptScript runs when a context menu is triggered
onfocusscriptFires when the element gets focus
oninputscriptScript runs when an element receives user input
oninvalidscriptScript runs when an element is invalid
onresetscriptFires when the Reset button in a form is clicked
onsearchscriptFires when the user writes in a search field (<input type="search">)
onselectscriptFires after text has been selected in an element
onsubmitscriptFires when a form is submitted

Keyboard Events

AttributeValueDescription
onkeydownscriptFires when a user is pressing a key
onkeypressscriptFires when a user presses a key
onkeyupscriptFires when a user releases a key

Mouse Events

AttributeValueDescription
onclickscriptFires on a mouse click on the element
ondblclickscriptFires on a mouse double-click on the element
onmousedownscriptFires when a mouse button is pressed down on an element
onmousemovescriptFires when the mouse pointer is moving over an element
onmouseoutscriptFires when the mouse pointer moves out of an element
onmouseoverscriptFires when the mouse pointer moves over an element
onmouseupscriptFires when a mouse button is released over an element
onmousewheelscriptDeprecated. Use the onwheel attribute instead
onwheelscriptFires when the mouse wheel rolls up or down over an element

Drag Events

AttributeValueDescription
ondragscriptScript to be run when an element is dragged
ondragendscriptScript to be run at the end of a drag operation
ondragenterscriptScript to be run when an element is dragged to a valid drop target
ondragleavescriptScript to be run when an element leaves a valid drop target
ondragoverscriptScript to be run when an element is being dragged over a valid drop target
ondragstartscriptScript to be run at the start of a drag operation
ondropscriptScript to be run when a dragged element is being dropped
onscrollscriptScript to be run when an element’s scrollbar is being scrolled

Clipboard Events

AttributeValueDescription
oncopyscriptFires when the user copies the content of an element
oncutscriptFires when the user cuts the content of an element
onpastescriptFires when the user pastes some content in an element

Media Events

These events occur when media elements such as videos, images, and audio are interacted with. While they apply to all HTML elements, they are most commonly used with media elements like audio, embed,img, object, and video.

Refer to the HTML Audio and Video DOM Reference for additional details.
AttributeValueDescription
onabortscriptScript to be run on abort
oncanplayscriptScript to be run when a file is ready to start playing (when it has buffered enough to begin)
oncanplaythroughscriptScript to be run when a file can be played all the way to the end without pausing for buffering
oncuechangescriptScript to be run when the cue changes in a track element
ondurationchangescriptScript to be run when the length of the media changes
onemptiedscriptScript to be run when something bad happens and the file is suddenly unavailable (like unexpectedly disconnects)
onendedscriptScript to be run when the media has reach the end (a useful event for messages like "thanks for listening")
onerrorscriptScript to be run when an error occurs when the file is being loaded
onloadeddatascriptScript to be run when media data is loaded
onloadedmetadatascriptScript to be run when meta data (like dimensions and duration) are loaded
onloadstartscriptScript to be run just as the file begins to load before anything is onerror actually loaded
onpausescriptScript to be run when the media is paused either by the user or programmatically
onplayscriptScript to be run when the media is ready to start playing
onplayingscriptScript to be run when the media actually has started playing
onprogressscriptScript to be run when the browser is in the process of getting the media data
onratechangescriptScript to be run each time the playback rate changes (like when a user switches to a slow motion or fast forward mode)
onseekedscriptScript to be run when the seeking attribute is set to false indicating that seeking has ended
onseekingscriptScript to be run when the seeking attribute is set to true indicating that seeking is active
onstalledscriptScript to be run when the browser is unable to fetch the media data for onprogress whatever reason
onsuspendscriptScript to be run when fetching the media data is stopped before it is onprogress completely loaded for whatever reason
ontimeupdatescriptScript to be run when the playing position has changed (like when the user onprogress fast forwards to a different point in the media)
onvolumechangescriptScript to be run each time the volume is changed (which includes setting onprogress the volume to "mute")
onwaitingscriptScript to be run when the media has paused but is expected to resume (like when the media pauses to buffer more data)