JavaScript MouseEvent Constructor

Beginner
⏱️ 12 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Web API constructor

What You’ll Learn

The MouseEvent() constructor builds a MouseEvent you can read or send with dispatchEvent(). Learn type strings, coordinates, modifier keys, button / buttons, and synthetic clicks—with five examples and try-it labs.

01

Create

new MouseEvent()

02

Type

click, mousemove…

03

Coords

clientX / screenX

04

Modifiers

ctrl / shift / alt

05

Buttons

button & buttons

06

Status

Baseline widely

Introduction

Real mouse clicks create MouseEvent objects automatically. Sometimes you need to build one yourself—for tests, demos, or triggering the same listeners without a physical click.

new MouseEvent("click", options) returns that object. Pair it with element.dispatchEvent(event) to run handlers as if the user clicked (when the event is configured to bubble/cancel like a real one).

💡
Beginner tip

Setting clientX / screenX only fills properties on the event object. Per MDN, it does not move the mouse pointer.

Understanding the MouseEvent() Constructor

You pass an event type string and an optional options object. Options include MouseEvent-specific fields plus those from UIEvent / Event (for example bubbles, cancelable, view, detail).

  • type — case-sensitive name such as "click".
  • CoordinatesclientX/clientY, screenX/screenY (default 0).
  • ModifiersctrlKey, shiftKey, altKey, metaKey (default false).
  • Buttonsbutton (which changed) and buttons (bit-field of held buttons).
  • relatedTarget — useful for enter/leave / over/out style events.

📝 Syntax

JavaScript
new MouseEvent(type)
new MouseEvent(type, options)

Parameters

  • type — String event name (case-sensitive). Common values: click, dblclick, mousedown, mouseenter, mouseleave, mousemove, mouseout, mouseover, mouseup.
  • options (optional) — Object with UIEvent properties plus MouseEvent fields below.

MouseEvent option fields (MDN)

OptionDefaultMeaning
screenX / screenY0Position on the user’s screen
clientX / clientY0Position in the viewport / client area
ctrlKey, shiftKey, altKey, metaKeyfalseModifier keys pressed
button00 left, 1 middle, 2 right
buttons0Bit-field: 1 left, 2 right, 4 middle
relatedTargetnullRelated element for over/out / enter/leave
regionnullHit-region id, or null

Return value

A new MouseEvent object.

🖱️ button vs buttons

buttonMeaning
0Main (usually left) or uninitialized
1Auxiliary (usually middle)
2Secondary (usually right)
buttons bitMeaning
0No button pressed
1Main (left) held
2Secondary (right) held
4Auxiliary (middle) held

⚡ Quick Reference

GoalCode
Create a click eventnew MouseEvent("click")
With coordinatesnew MouseEvent("click", { clientX: 40, clientY: 12 })
Bubbling synthetic clicknew MouseEvent("click", { bubbles: true, cancelable: true, view: window })
Fire on an elementel.dispatchEvent(event)
Simulate Ctrl+click{ ctrlKey: true, bubbles: true }
MDN statusBaseline Widely available (since July 2015)

🔍 At a Glance

Four facts to remember about new MouseEvent().

Returns
MouseEvent

Event object

Baseline
widely

Since July 2015

Coords
do not move

Pointer stays put

Dispatch
dispatchEvent

To fire handlers

Examples Gallery

Examples follow MDN MouseEvent(). Use View Output or Try It Yourself.

📚 Getting Started

Create events and set coordinates.

Example 1 — Create a Basic click Event

Minimal constructor call with only a type string.

JavaScript
const event = new MouseEvent("click");

console.log(event.type);     // "click"
console.log(event.clientX);  // 0 (default)
console.log(event.button);   // 0 (default)
console.log(event instanceof MouseEvent); // true
Try It Yourself

How It Works

Without options, numeric fields default to 0 and modifier keys to false. The object is ready to inspect or dispatch.

Example 2 — Set clientX and clientY

Fill viewport coordinates (the pointer itself does not move).

JavaScript
const event = new MouseEvent("mousemove", {
  clientX: 120,
  clientY: 45,
  screenX: 500,
  screenY: 300
});

console.log(event.clientX, event.clientY); // 120 45
console.log(event.screenX, event.screenY); // 500 300
Try It Yourself

How It Works

Handlers that read event.clientX will see your values. The OS cursor position is unchanged.

📈 Modifiers, Buttons & Dispatch

Keys, button fields, and firing a synthetic click.

Example 3 — Modifier Keys

Simulate a Ctrl+Shift click for tests or demos.

JavaScript
const event = new MouseEvent("click", {
  ctrlKey: true,
  shiftKey: true,
  altKey: false,
  metaKey: false
});

console.log(event.ctrlKey);   // true
console.log(event.shiftKey);  // true
console.log(event.altKey);    // false
console.log(event.metaKey);   // false
Try It Yourself

How It Works

Listeners that branch on event.ctrlKey (open in new tab patterns, multi-select, and so on) can be exercised without holding physical keys.

Example 4 — button and buttons

Describe which button changed vs which are held.

JavaScript
const leftDown = new MouseEvent("mousedown", {
  button: 0,   // left changed
  buttons: 1   // left currently held
});

const rightClick = new MouseEvent("mousedown", {
  button: 2,   // right changed
  buttons: 2   // right held
});

console.log(leftDown.button, leftDown.buttons);   // 0 1
console.log(rightClick.button, rightClick.buttons); // 2 2
Try It Yourself

How It Works

Use button for “which button just changed,” and buttons for the current held set (combine bits with | when needed).

Example 5 — Dispatch a Synthetic Click

Create a bubbling click and fire it on a button.

JavaScript
const btn = document.createElement("button");
btn.textContent = "Go";
btn.addEventListener("click", (e) => {
  console.log("clicked via", e.type, "clientX=", e.clientX);
});

const event = new MouseEvent("click", {
  bubbles: true,
  cancelable: true,
  view: window,
  clientX: 88,
  clientY: 16
});

btn.dispatchEvent(event);
// clicked via click clientX= 88
Try It Yourself

How It Works

dispatchEvent delivers your constructed event to listeners. Set bubbles: true if parent handlers should see it too.

🚀 Common Use Cases

  • Unit/UI tests that need a realistic mouse event object.
  • Demo buttons that trigger the same click handlers as a real click.
  • Simulating modifier-key clicks (Ctrl/Shift) in tutorials or automation helpers.
  • Feeding coordinates into code that reads clientX / clientY.
  • Teaching how EventTarget dispatchEvent works with MouseEvent.

🔧 How It Works

1

Choose a type

Pass a case-sensitive string such as "click".

Type
2

Fill options

Coords, modifiers, buttons, bubbles, view, and more.

Options
3

Get a MouseEvent

Read properties or prepare to dispatch.

Object
4

Optional: dispatch

Call target.dispatchEvent(event) to run listeners.

📝 Notes

Universal Browser Support

The MouseEvent() constructor is Baseline Widely available across modern browsers (MDN: since July 2015). Logos use the shared browser-image-sprite.png sprite from this project.

Baseline · Widely available

MouseEvent() constructor

Safe API for building synthetic mouse events and dispatching them in tests or demos.

Universal Widely available
Google Chrome Full support · Desktop & Mobile
Full support
Mozilla Firefox Full support · Desktop & Mobile
Full support
Apple Safari Full support · macOS & iOS
Full support
Microsoft Edge Full support · Chromium
Full support
Opera Full support · Modern versions
Full support
Internet Explorer Supported in modern IE versions
Full support
MouseEvent() Excellent

Bottom line: Create MouseEvent objects with type + options; dispatch with EventTarget.dispatchEvent.

Conclusion

new MouseEvent(type, options) builds a mouse event with the coordinates, keys, and buttons you choose. Dispatch it to exercise listeners—remember that coordinates do not move the real pointer.

Continue with WEBKIT_FORCE_AT_FORCE_MOUSE_DOWN, dispatchEvent(), Window, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Set bubbles / cancelable when mimicking real clicks
  • Pass numeric client/screen values only
  • Use view: window for UIEvent-compatible synthetic clicks
  • Test handlers with both real and constructed events
  • Learn button vs buttons separately

❌ Don’t

  • Expect coordinates to move the OS cursor
  • Misspell type strings ("Click""click")
  • Forget dispatchEvent if you need handlers to run
  • Confuse button (changed) with buttons (held)
  • Skip accessibility—prefer real user input when possible

Key Takeaways

Knowledge Unlocked

Five things to remember about MouseEvent()

Build synthetic mouse events, then dispatch when needed.

5
Core concepts
📍 02

Coords

don’t move pointer

MDN
⌨️ 03

Modifiers

ctrl / shift / alt

Keys
🖱 04

button(s)

changed vs held

Fields
🔔 05

dispatchEvent

fire handlers

Use

❓ Frequently Asked Questions

new MouseEvent(type, options) creates a MouseEvent object you can inspect or dispatch with element.dispatchEvent(event). It does not move the real mouse pointer.
No. MDN marks the MouseEvent() constructor as Baseline Widely available (since July 2015). It is a standard DOM constructor — not Deprecated, Experimental, or Non-standard.
The type is a case-sensitive string. Browsers commonly set click, dblclick, mousedown, mouseenter, mouseleave, mousemove, mouseout, mouseover, or mouseup.
No. MDN notes that setting screen/client coordinates only fills event properties — they do not move the mouse pointer on screen.
button describes which button changed for press/release-related events (0 left, 1 middle, 2 right). buttons is a bit-field of which buttons are currently held (1 left, 2 right, 4 middle).
Create the event with new MouseEvent("click", { bubbles: true, cancelable: true, view: window }), then call target.dispatchEvent(event). Listeners on that element (and ancestors if it bubbles) will run.
Did you know?

MDN documents MouseEvent under Pointer Events for the constructor specification link, while the interface itself is the long-standing mouse event model. Synthetic events are great for tests—but trusted user gestures (like opening a file picker) may still require a real click in some browsers.

Next: WEBKIT_FORCE_AT_FORCE_MOUSE_DOWN

Learn the non-standard WebKit static force-click threshold.

Force threshold →

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.

5 people found this page helpful