Definition:
In HTML, events are actions or occurrences that happen in the browser, which can be detected and responded to using JavaScript.
Examples: Clicking a button, loading a page, submitting a form, moving the mouse, typing on the keyboard, etc.

Events allow HTML elements to be interactive and dynamic.


1. How Events Work

  • An event happens (e.g., user clicks a button).
  • An event handler (a JavaScript function) is triggered.
  • The handler runs code in response to the event.

Example:

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

Here:

  • onclick → event attribute
  • alert(...) → event handler function

2. Main Types of HTML Events (with Examples)

A) Mouse Events

EventDescriptionExample
onclickFires when element is clicked<button onclick="...">
ondblclickFires on double-click<div ondblclick="...">
onmousedownMouse button pressed<img onmousedown="...">
onmouseupMouse button released<p onmouseup="...">
onmouseoverPointer enters element<span onmouseover="...">
onmouseoutPointer leaves element<span onmouseout="...">
onmousemovePointer moves over element<div onmousemove="...">

B) Keyboard Events

EventDescriptionExample
onkeydownKey pressed<input onkeydown="...">
onkeypressKey pressed and held (deprecated, use keydown)<input onkeypress="...">
onkeyupKey released<input onkeyup="...">

C) Form Events

EventDescriptionExample
onsubmitForm submitted<form onsubmit="...">
onresetForm reset<form onreset="...">
onchangeValue changed (after losing focus)<input onchange="...">
oninputValue changes instantly while typing<textarea oninput="...">
onfocusElement gets focus<input onfocus="...">
onblurElement loses focus<input onblur="...">

D) Window / Document Events

EventDescriptionExample
onloadPage loaded<body onload="...">
onunloadPage unloaded/closed<body onunload="...">
onresizeBrowser window resized<body onresize="...">
onscrollPage/element scrolled<div onscroll="...">

E) Drag & Drop Events

EventDescription
ondragElement is being dragged
ondragstartDragging starts
ondragendDragging ends
ondragoverDragging over a target
ondropDropped on target

3. Example – Using Different Events

<!DOCTYPE html>
<html>
<body>

<h2>HTML Events Example</h2>

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

<!-- Keyboard Event -->
<input type="text" onkeyup="console.log('Key released!')">

<!-- Form Event -->
<form onsubmit="alert('Form Submitted!'); return false;">
  <input type="text" placeholder="Enter name">
  <button type="submit">Submit</button>
</form>

<!-- Window Event -->
<body onload="console.log('Page Loaded!')">

</body>
</html>

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *