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
Event Description Example onclick
Fires when element is clicked <button onclick="...">
ondblclick
Fires on double-click <div ondblclick="...">
onmousedown
Mouse button pressed <img onmousedown="...">
onmouseup
Mouse button released <p onmouseup="...">
onmouseover
Pointer enters element <span onmouseover="...">
onmouseout
Pointer leaves element <span onmouseout="...">
onmousemove
Pointer moves over element <div onmousemove="...">
B) Keyboard Events
Event Description Example onkeydown
Key pressed <input onkeydown="...">
onkeypress
Key pressed and held (deprecated, use keydown
) <input onkeypress="...">
onkeyup
Key released <input onkeyup="...">
C) Form Events
Event Description Example onsubmit
Form submitted <form onsubmit="...">
onreset
Form reset <form onreset="...">
onchange
Value changed (after losing focus) <input onchange="...">
oninput
Value changes instantly while typing <textarea oninput="...">
onfocus
Element gets focus <input onfocus="...">
onblur
Element loses focus <input onblur="...">
D) Window / Document Events
Event Description Example onload
Page loaded <body onload="...">
onunload
Page unloaded/closed <body onunload="...">
onresize
Browser window resized <body onresize="...">
onscroll
Page/element scrolled <div onscroll="...">
E) Drag & Drop Events
Event Description ondrag
Element is being dragged ondragstart
Dragging starts ondragend
Dragging ends ondragover
Dragging over a target ondrop
Dropped 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>