HTML Event Attributes

HTML Event Attributes are special attributes used to execute JavaScript code when a specific action or event occurs in the browser. These events can happen when a user clicks a button, types in an input field, moves the mouse, submits a form, or when a webpage finishes loading.

HTML events make webpages interactive and dynamic by allowing developers to respond to user actions instantly.

An HTML Event Attribute is added directly inside an HTML element to trigger JavaScript code whenever a particular event occurs.

Example of HTML Event Attribute

<button onclick="showMessage()">
Click Me
</button>

<script>
function showMessage() {
  alert("Button Clicked");
}
</script>

In the above example, the onclick event runs JavaScript code when the button is clicked.

Below are the global event attributes that can be added to HTML elements to define event actions.

Window Event Attributes

Events triggered for the browser window object.

AttributeValueDescription
onafterprintscriptScript to run after the document is printed
onbeforeprintscriptScript to run before the document is printed
onbeforeunloadscriptScript to run before the document is unloaded
onerrorscriptScript to run when an error occurs
onhashchangescriptScript to run when URL hash changes
onloadscriptFires after the page finishes loading
onmessagescriptScript to run when a message is received
onofflinescriptScript to run when browser goes offline
ononlinescriptScript to run when browser goes online
onpagehidescriptScript to run when user leaves the page
onpageshowscriptScript to run when user opens the page
onpopstatescriptScript to run when browser history changes
onresizescriptFires when browser window is resized
onstoragescriptScript to run when Web Storage updates
onunloadscriptFires when the page unloads

Form Event Attributes

Events triggered by actions inside HTML forms and input elements.

AttributeValueDescription
onblurscriptFires when element loses focus
onchangescriptFires when element value changes
oncontextmenuscriptFires when context menu appears
onfocusscriptFires when element gets focus
oninputscriptFires when user inputs data
oninvalidscriptFires when element is invalid
onresetscriptFires when form resets
onsearchscriptFires when user searches
onselectscriptFires after text selection
onsubmitscriptFires when form is submitted

Keyboard Event Attributes

Events triggered by keyboard actions.

AttributeValueDescription
onkeydownscriptFires when key is pressed
onkeypressscriptFires when key is pressed down
onkeyupscriptFires when key is released

Mouse Event Attributes

Events triggered by mouse actions.

AttributeValueDescription
onclickscriptFires when element is clicked
ondblclickscriptFires when element is double-clicked
onmousedownscriptFires when mouse button is pressed
onmousemovescriptFires when mouse moves over element
onmouseoutscriptFires when mouse leaves element
onmouseoverscriptFires when mouse enters element
onmouseupscriptFires when mouse button is released
onmousewheelscriptDeprecated. Use onwheel instead
onwheelscriptFires when mouse wheel scrolls

Drag Event Attributes

Events triggered during drag-and-drop operations.

AttributeValueDescription
ondragscriptFires while element is dragged
ondragendscriptFires after drag operation ends
ondragenterscriptFires when dragged item enters target
ondragleavescriptFires when dragged item leaves target
ondragoverscriptFires while dragging over target
ondragstartscriptFires when drag operation starts
ondropscriptFires when dragged item is dropped
onscrollscriptFires when scrollbar is scrolled

Clipboard Event Attributes

Events triggered during copy, cut, and paste actions.

AttributeValueDescription
oncopyscriptFires when content is copied
oncutscriptFires when content is cut
onpastescriptFires when content is pasted

Media Event Attributes

Events triggered by media elements like audio, video, and images.

AttributeValueDescription
onabortscriptFires on abort
oncanplayscriptFires when media can start playing
oncanplaythroughscriptFires when media can play without buffering
oncuechangescriptFires when cue changes
ondurationchangescriptFires when media duration changes
onemptiedscriptFires when media becomes unavailable
onendedscriptFires when media playback ends
onerrorscriptFires when media loading error occurs
onloadeddatascriptFires when media data loads
onloadedmetadatascriptFires when media metadata loads
onloadstartscriptFires when media loading starts
onpausescriptFires when media pauses
onplayscriptFires when media starts playing
onplayingscriptFires when media is actively playing
onprogressscriptFires while media downloads
onratechangescriptFires when playback speed changes
onseekedscriptFires after seeking ends
onseekingscriptFires while seeking
onstalledscriptFires when browser cannot fetch media
onsuspendscriptFires when media loading stops
ontimeupdatescriptFires when playback position changes
onvolumechangescriptFires when volume changes
onwaitingscriptFires while waiting for media buffering

Misc Event Attributes

AttributeValueDescription
ontogglescriptFires when <details> element opens or closes

HTML Event Attributes are supported in all modern browsers including:

  • Google Chrome
  • Mozilla Firefox
  • Microsoft Edge
  • Safari
  • Opera

Leave a Comment