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
Fundamentals
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.
Concept
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).
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
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.
UniversalWidely available
Google ChromeFull support · Desktop & Mobile
Full support
Mozilla FirefoxFull support · Desktop & Mobile
Full support
Apple SafariFull support · macOS & iOS
Full support
Microsoft EdgeFull support · Chromium
Full support
OperaFull support · Modern versions
Full support
Internet ExplorerSupported in modern IE versions
Full support
MouseEvent()Excellent
Bottom line: Create MouseEvent objects with type + options; dispatch with EventTarget.dispatchEvent.
Wrap Up
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.
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
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about MouseEvent()
Build synthetic mouse events, then dispatch when needed.
5
Core concepts
🖱️01
new MouseEvent
type + options
API
📍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.