JavaScript MouseEvent

Beginner
8 min read
Updated: Nov 2026
3 examples
Pointer clicks

Definition and Usage

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.

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);
});

Core Ideas

Ideas every MouseEvent tutorial builds on:

IdeaExampleRemember
Extends Eventevent.typeStill has type and target
Viewport coordsevent.clientXRelative to visible viewport
Page coordsevent.pageXIncludes scroll offset
Buttonevent.button0 left, 1 middle, 2 right
Modifiersevent.shiftKeyShift / Ctrl / Alt / Meta
relatedTargetevent.relatedTargetOther node for over/out

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);
});

How It Works

  1. click delivers a MouseEvent.
  2. clientX / clientY are viewport pixels.
  3. 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);
});

How It Works

  1. mousedown fires for each button press.
  2. 0 means the main button.
  3. Use contextmenu for right-click menus when needed.

Example 3 — Create a MouseEvent

You can synthesize clicks for tests with the constructor.

JavaScript
const event = new MouseEvent("click", { bubbles: true, clientX: 10, clientY: 20 });
console.log(event.type);
console.log(event.clientX);

How It Works

  1. new MouseEvent("click", options) builds the event.
  2. bubbles: true lets it travel up the tree.
  3. clientX is set from the options object.

Next: MouseEvent Guides

Continue with the constructor and coordinate properties.

Look upWhy
ConstructorCreate MouseEvent objects
clientX / clientYViewport coordinates
buttonWhich button was used
getModifierState()Check Shift, Ctrl, and more

Full lists: MouseEvent constructor.

Frequently Asked Questions

A MouseEvent is the event object for mouse actions such as click and mousemove. It adds coordinates and button data on top of the base Event fields.
clientX is relative to the visible viewport. pageX includes document scroll, so it stays tied to the page layout.
0 is usually the left/main button, 1 the middle button, and 2 the right button. Check buttons (plural) when more than one may be held.
Many browsers also synthesize click MouseEvents after taps for compatibility. Prefer Pointer Events for new multi-input work.
Open the MouseEvent constructor, then Event and EventTarget for the shared event model.

Open the MouseEvent constructor

Then explore clientX, button, and modifier helpers.

MouseEvent constructor →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful