💡 Tip: Login now to track your course progress and unlock your downloadable certificates instantly!

Event Attributes

HTML event attributes allow you to add interactivity to your webpage.
They define how elements respond when certain events occur — such as when a user clicks a button, hovers over an image, submits a form, presses a key, or moves the mouse.

In short, event attributes connect HTML with JavaScript to make webpages dynamic and interactive.

What is an Event?

An event is an action that happens in the browser — triggered by the user or the browser itself.

Examples of events:

  • A user clicks a button → onclick
  • A webpage finishes loading → onload
  • A user presses a key → onkeydown
  • A form is submitted → onsubmit
  • A mouse pointer hovers over an element → onmouseover

Basic Syntax

<element event="JavaScript code"></element>

Example:

<button onclick="alert('Button clicked!')">Click Me</button>

When the user clicks this button, an alert box will pop up.

Common Event Attributes

Event AttributeDescriptionExample
onclickTriggered when an element is clicked<button onclick="myFunction()">Click</button>
onloadTriggered when the page or image finishes loading<body onload="init()">
onchangeTriggered when a form element value changes<input onchange="checkValue()">
onmouseoverTriggered when the mouse pointer hovers over an element<div onmouseover="highlight()">Hover</div>
onmouseoutTriggered when the mouse pointer leaves the element<div onmouseout="removeHighlight()">Move out</div>
onkeydownTriggered when a keyboard key is pressed<input onkeydown="keyPress()">
onkeyupTriggered when a keyboard key is released<input onkeyup="showText()">
onfocusTriggered when an input field gets focus<input onfocus="highlightField()">
onblurTriggered when an input field loses focus<input onblur="removeHighlight()">
onsubmitTriggered when a form is submitted<form onsubmit="return validateForm()">

Example 1: Click Event

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Event Example</title>
  <script>
    function showMessage() {
      alert("You clicked the button!");
    }
  </script>
</head>
<body>
  <button onclick="showMessage()">Click Me</button>
</body>
</html>

Example 2: Form Validation Event

<form onsubmit="return validateForm()">
  <input type="text" id="name" placeholder="Enter your name">
  <button type="submit">Submit</button>
</form>

<script>
  function validateForm() {
    let name = document.getElementById('name').value;
    if (name === "") {
      alert("Please enter your name!");
      return false;
    }
    alert("Form submitted successfully!");
    return true;
  }
</script>

The form will only submit if the name field is filled.

Example 3: Mouse Events

<div onmouseover="this.style.background='lightgreen'" 
     onmouseout="this.style.background='white'"
     style="width:200px; height:100px; border:1px solid gray; text-align:center; line-height:100px;">
  Hover over me!
</div>

The background color changes when you hover your mouse.

Event Categories

CategoryExamples
Mouse Eventsonclick, ondblclick, onmouseover, onmouseout, onmousemove
Keyboard Eventsonkeydown, onkeypress, onkeyup
Form Eventsonsubmit, onchange, onfocus, onblur
Window Eventsonload, onresize, onscroll, onunload

Best Practice

While you can write JavaScript inside HTML event attributes, it’s better to separate JavaScript from HTML for cleaner, maintainable code.

Example (better approach):

<button id="clickBtn">Click Me</button>

<script>
document.getElementById("clickBtn").onclick = function() {
  alert("Hello, World!");
};
</script>

Complete HTML Event Attributes Reference

Mouse Events

Event AttributeDescription
onclickTriggered when an element is clicked.
ondblclickTriggered when an element is double-clicked.
onmousedownTriggered when a mouse button is pressed down.
onmouseupTriggered when a mouse button is released.
onmouseoverTriggered when the mouse pointer enters an element.
onmouseoutTriggered when the mouse pointer leaves an element.
onmousemoveTriggered when the mouse is moved within an element.
onwheelTriggered when the mouse wheel is scrolled.
oncontextmenuTriggered when the right mouse button is clicked (opens context menu).

Keyboard Events

Event AttributeDescription
onkeydownTriggered when a key is pressed down.
onkeypressTriggered when a key is pressed (deprecated in HTML5).
onkeyupTriggered when a key is released.

Form Events

Event AttributeDescription
oninputTriggered when user input is received.
onchangeTriggered when an element’s value changes.
onsubmitTriggered when a form is submitted.
onresetTriggered when a form is reset.
onfocusTriggered when an element receives focus.
onblurTriggered when an element loses focus.
onselectTriggered when text within an input or textarea is selected.
oninvalidTriggered when a form field fails validation.

Window / Document Events

Event AttributeDescription
onloadTriggered when the page or element is fully loaded.
onunloadTriggered when the page is closed or refreshed.
onresizeTriggered when the browser window is resized.
onscrollTriggered when the user scrolls an element or page.
onerrorTriggered when an error occurs while loading an element (like an image).

Drag & Drop Events

Event AttributeDescription
ondragTriggered while an element is being dragged.
ondragstartTriggered when drag starts.
ondragendTriggered when drag ends.
ondragenterTriggered when dragged element enters a valid drop target.
ondragleaveTriggered when dragged element leaves a drop target.
ondragoverTriggered when dragged element is over a drop target.
ondropTriggered when dragged element is dropped.

Clipboard Events

Event AttributeDescription
oncopyTriggered when text or content is copied.
oncutTriggered when text or content is cut.
onpasteTriggered when text or content is pasted.

Media Events (Audio/Video)

Event AttributeDescription
onplayTriggered when media starts playing.
onpauseTriggered when media is paused.
onendedTriggered when media playback ends.
onvolumechangeTriggered when the volume changes.
ontimeupdateTriggered when the playback position changes.
onloadeddataTriggered when media data is loaded.
onprogressTriggered while downloading media data.
onerrorTriggered when media loading fails.

Animation & Transition Events

Event AttributeDescription
onanimationstartTriggered when a CSS animation starts.
onanimationendTriggered when a CSS animation ends.
onanimationiterationTriggered when a CSS animation repeats.
ontransitionendTriggered when a CSS transition finishes.

Clipboard & Input Composition Events

Event AttributeDescription
oncompositionstartTriggered when text composition (IME) starts.
oncompositionupdateTriggered when text composition is updated.
oncompositionendTriggered when text composition ends.

Touch & Pointer Events (Mobile)

Event AttributeDescription
ontouchstartTriggered when a touch point is placed on the touch surface.
ontouchmoveTriggered when a touch point moves.
ontouchendTriggered when a touch point is removed.
onpointerdownTriggered when a pointer (mouse, pen, or touch) is pressed.
onpointerupTriggered when a pointer is released.
onpointermoveTriggered when a pointer moves.

Miscellaneous Events

Event AttributeDescription
onabortTriggered when media loading is aborted.
onbeforeunloadTriggered before a document is unloaded.
onhashchangeTriggered when the URL hash changes.
onmessageTriggered when a message is received via postMessage().
onpopstateTriggered when navigating browser history.
onstorageTriggered when data in localStorage or sessionStorage changes.

Conclusion

Event attributes are essential for connecting HTML with JavaScript.
Understanding them enables you to build interactive, responsive, and dynamic web pages that respond to user actions and system changes effectively.

Sign In Form

User your email and password to singn in

Don’t have an account, signup here : 

HTML5 & CSS3

Tools and Tutorials

Want a Website

Want a website for your business ?

Please fill out the details below, so we can reach out to you.

Registration Form

Signup to track your record and much more.

We have sent you an email with a registration link. Please click the link to verify your email address, or enter the 6-digit OTP provided in the email.

Didn't receive the OTP. Regenerate OTP Resend