Events

HTML events are actions that trigger JavaScript code, such as clicks, key presses, or page loads, allowing for dynamic behavior and interactivity on web pages.

HTML Events Overview

HTML events represent user interactions or browser activities, such as mouse clicks, keyboard inputs, or page loads. These events can trigger JavaScript functions to handle user input. Examples include onclick, onmouseover, and onchange.

Using Inline Event Handlers in HTML

Inline event handlers embed JavaScript directly within HTML elements using attributes like onclick and onmouseover. These handlers trigger actions when specific events occur.

Syntax

index.html
<element event="handler">

Example

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

Explanation

When the button is clicked, the onclick event triggers the alert() function, displaying "Button clicked!"

Event Categories

Window Events

These events occur at the window level, often used with the <body> tag.

AttributeDescription
onafterprintFires after the document is printed
onbeforeprintFires before the document is printed
onbeforeunloadFires when the document is about to be unloaded
onerrorFires when an error occurs
onhashchangeFires when the URL's anchor part changes
onloadFires when the page loads
onresizeFires when the browser window is resized
onunloadFires when a page is unloaded

Form Events

Triggered by interactions within form elements.

AttributeDescription
onblurFires when an element loses focus
onchangeFires when an element's value changes
onfocusFires when an element gains focus
oninputFires when the user inputs data
oninvalidFires when an input is invalid
onresetFires when a form reset occurs
onsubmitFires when a form is submitted

Keyboard Events

AttributeDescription
onkeydownFires when a key is pressed down
onkeypressDeprecated - Use onkeydown instead
onkeyupFires when a key is released

Mouse Events

AttributeDescription
onclickFires on a mouse click
ondblclickFires on a double-click
onmousedownFires when the mouse button is pressed
onmousemoveFires when the mouse moves
onmouseoutFires when the mouse leaves an element
onmouseoverFires when the mouse enters an element
onmouseupFires when a mouse button is released
onwheelFires when the mouse wheel scrolls

Drag-Drop Events

AttributeDescription
ondragFires when an element is dragged
ondragendFires when a drag operation ends
ondragenterFires when a dragged element enters a drop target
ondragleaveFires when a dragged element leaves a drop target
ondragoverFires when a dragged element is over a drop target
ondragstartFires when dragging starts
ondropFires when an element is dropped

Clipboard Events

AttributeDescription
oncopyFires when content is copied
oncutFires when content is cut
onpasteFires when content is pasted

Media Events

These events apply mainly to media elements like <audio>, <video>, and <img>.

AttributeDescription
onabortFires when media loading is aborted
oncanplayFires when media can start playing
onendedFires when media playback ends
onpauseFires when media is paused
onplayFires when media starts playing
onvolumechangeFires when volume changes
onwaitingFires when buffering occurs

Conclusion

HTML events are crucial for enabling interactivity on web pages. They represent user actions or browser activities like clicks, key presses, or page loads, which can trigger JavaScript functions to handle the input. Events can be embedded directly into HTML elements using attributes such as ondblclick, onmouseover, and onchange.