jQuery Mouse Events
Mouse events fire when a user interacts with the page using a mouse — clicking, hovering, moving, or pressing mouse buttons. jQuery provides simple methods to handle all these actions. Learning mouse events lets you build interactive menus, tooltips, drag effects, and much more.
Common jQuery Mouse Events
| Event Method | Fires When |
|---|---|
| click() | Mouse button is clicked |
| dblclick() | Mouse button is double-clicked |
| mouseenter() | Mouse pointer enters an element |
| mouseleave() | Mouse pointer leaves an element |
| mouseover() | Mouse pointer moves over element or its children |
| mouseout() | Mouse pointer moves out of element or its children |
| mousedown() | Mouse button is pressed down |
| mouseup() | Mouse button is released |
| mousemove() | Mouse pointer moves inside an element |
| hover() | Shorthand for mouseenter + mouseleave |
Diagram: Mouse Event Timeline
User Action: Move mouse → Enter element → Click → Release → Move out Events fired: mousemove → mouseenter → mouseover → mousedown → mouseup → click → mouseleave → mouseout
click() — Most Used Event
$(document).ready(function() {
$("#submitBtn").click(function() {
alert("Form submitted!");
});
});
The click() method attaches a function that runs every time the element is clicked.
dblclick() — Double Click
$(document).ready(function() {
$("img").dblclick(function() {
$(this).css("border", "3px solid orange");
});
});
Double-clicking any image adds an orange border to it. $(this) refers to the element that was double-clicked.
mouseenter() and mouseleave()
These are the cleanest events for hover effects. They do NOT fire when the mouse moves over child elements inside the target.
$(document).ready(function() {
$(".card").mouseenter(function() {
$(this).css("box-shadow", "0 4px 8px rgba(0,0,0,0.3)");
});
$(".card").mouseleave(function() {
$(this).css("box-shadow", "none");
});
});
hover() — Shorthand for mouseenter + mouseleave
The hover() method takes two functions — one for entering, one for leaving. It reduces repetition.
$(document).ready(function() {
$(".menu-item").hover(
function() {
$(this).css("background", "#007bff"); // mouse enters
},
function() {
$(this).css("background", ""); // mouse leaves
}
);
});
Diagram: hover() Flow
Mouse moves over .menu-item
│
▼
First function runs → background turns blue
Mouse moves away from .menu-item
│
▼
Second function runs → background resets
mouseover() vs mouseenter() — Key Difference
This is a common source of confusion. The difference is about child elements.
HTML:
<div id="outer">
<p>Child paragraph</p>
</div>
mouseover: fires when you enter #outer AND when you enter the <p> inside it
mouseenter: fires ONLY when you enter #outer — ignores child elements
Use mouseenter and mouseleave for most hover effects to avoid unwanted repeated firing.
mousedown() and mouseup()
These events fire at the exact moment the mouse button goes down or comes back up — before a full click completes.
$(document).ready(function() {
$("#pressBtn").mousedown(function() {
$(this).css("background", "#cc0000"); // button pressed visual
});
$("#pressBtn").mouseup(function() {
$(this).css("background", ""); // button released
});
});
mousemove() — Track Mouse Position
The mousemove() event fires every time the mouse moves inside an element. The event object carries the mouse coordinates.
$(document).ready(function() {
$(document).mousemove(function(event) {
$("#coords").text("X: " + event.pageX + " | Y: " + event.pageY);
});
});
Diagram: Mouse Coordinate System
(0, 0) ─────────────────────────── X increases →
│
│ Mouse pointer moves here → pageX = 340, pageY = 120
│
Y increases ↓
Practical Example: Image Gallery Highlight
$(document).ready(function() {
$(".gallery-img").hover(
function() {
$(this).animate({ opacity: 1 }, 200);
},
function() {
$(this).animate({ opacity: 0.6 }, 200);
}
);
$(".gallery-img").click(function() {
var src = $(this).attr("src");
$("#preview").attr("src", src);
});
});
Hovering highlights an image, clicking shows it in a preview area.
Quick Summary
click()— fires on a full click (press + release).dblclick()— fires on double click.hover()— shorthand for mouseenter and mouseleave.mouseenter()/mouseleave()— clean hover without child element interference.mousedown()/mouseup()— fires at press and release separately.mousemove()— fires continuously as the mouse moves, provides X/Y coordinates.
