A MouseEvent is the event object for mouse (and many pointer) actions such as click, mousedown, and mousemove. It extends Event with coordinates and button information.
Common properties include clientX / clientY (position in the viewport), button (which button), and relatedTarget for enter/leave pairs. Listen with addEventListener("click", handler).
This page teaches what MouseEvent is. Constructor, methods, and properties live under this section.
click is a MouseEvent.
Your click handler’s event argument is a MouseEvent, so event.clientX works without casting.
Syntax
JavaScript Syntax
Reading a MouseEvent in a listener:
JavaScript
box.addEventListener("click", function (event) {
console.log(event.type);
console.log(event.clientX, event.clientY);
console.log(event.button);
});
Reference
Core Ideas
Ideas every MouseEvent tutorial builds on:
Idea
Example
Remember
Extends Event
event.type
Still has type and target
Viewport coords
event.clientX
Relative to visible viewport
Page coords
event.pageX
Includes scroll offset
Button
event.button
0 left, 1 middle, 2 right
Modifiers
event.shiftKey
Shift / Ctrl / Alt / Meta
relatedTarget
event.relatedTarget
Other node for over/out
Code
JavaScript Examples
Three short programs that show how MouseEvent works.
Example 1 — Log click coordinates
clientX and clientY are relative to the viewport.
JavaScript
el.addEventListener("click", function (event) {
console.log(event.clientX + "," + event.clientY);
});
Output
120,80
How It Works
click delivers a MouseEvent.
clientX / clientY are viewport pixels.
Values change when the user clicks elsewhere.
Example 2 — Which mouse button
button is 0 for the primary (usually left) button.
JavaScript
el.addEventListener("mousedown", function (event) {
console.log(event.button);
});
Output
0
How It Works
mousedown fires for each button press.
0 means the main button.
Use contextmenu for right-click menus when needed.
Example 3 — Create a MouseEvent
You can synthesize clicks for tests with the constructor.