JavaScript MouseEvent initmouse Event

Beginner
5 min read
Updated: Nov 2026
5 examples
Event

Definition

initMouseEvent(…) initializes a mouse event created with document.createEvent() before you dispatchEvent() it (MDN). Prefer new MouseEvent() instead.

Register a listener with addEventListener. Check readiness and browser support before you rely on this event.

Syntax

Listen for initmouse. Start with this line.

JavaScript
const e = document.createEvent("MouseEvents");
CallWhat it does
const e = document.createEvent("MouseEvents");Primary signature.

Built into the browser. Listen with event.addEventListener("initmouse", …) — no import required.

Library declarations
JavaScript
const e = document.createEvent("MouseEvents");

Parameters

ParameterDescription
typeRequired. click, mousedown, …
Flags / view / detailRequired. bubbles, cancelable, window, click count
Coords + modifiersRequired. screen/client XY, ctrl/alt/shift/meta
button, relatedTargetRequired. Button index; null for most types

Return Value

Return ValueDescription
*undefined (mutates in place)

JavaScript Examples

Example 1 — Legacy createEvent + initMouseEvent

MDN-style deprecated synthetic click.

JavaScript
const event = document.createEvent("MouseEvents");
event.initMouseEvent(
  "click", true, true, window, 0,
  0, 0, 80, 20,
  false, false, false, false,
  0, null
);
document.body.dispatchEvent(event);
console.log(event.type, event.clientX, event.clientY);
Try It Yourself

How It Works

  1. createEvent("MouseEvents"). Legacy factory.
  2. initMouseEvent(…). Fills all mouse fields.
  3. dispatchEvent. Delivers the synthetic click.
  4. Log type/coords. Confirms clientX/clientY.

Example 2 — Read Initialized Fields

After init, properties mirror the positional arguments.

JavaScript
const event = document.createEvent("MouseEvents");
event.initMouseEvent(
  "mousedown", true, false, window, 1,
  10, 20, 30, 40,
  true, false, false, false,
  0, null
);
console.log(event.type, event.detail, event.clientX, event.ctrlKey);
Try It Yourself

How It Works

  1. Pick a type. Here mousedown.
  2. Set detail / coords / ctrlKey in the long argument list.
  3. Read properties. They match what you passed.
  4. Order matters. Positional args are easy to misplace.

Example 3 — Modern MouseEvent()

Same idea with the constructor — preferred for new code.

JavaScript
const btn = document.getElementById("btn");
btn.addEventListener("click", (e) => {
  console.log(e.type, e.clientX, e.clientY);
});

const event = new MouseEvent("click", {
  bubbles: true,
  cancelable: true,
  clientX: 80,
  clientY: 20
});
btn.dispatchEvent(event);
Try It Yourself

How It Works

  1. Skip createEvent. No initMouseEvent.
  2. Named options. Clearer than 15 positional args.
  3. dispatchEvent as usual.
  4. Use this in new apps. Not deprecated.

Example 4 — Shift Modifier

Legacy list sets shiftKey; modern options do the same.

JavaScript
const legacy = document.createEvent("MouseEvents");
legacy.initMouseEvent(
  "click", true, true, window, 0,
  0, 0, 0, 0,
  false, false, true, false,
  0, null
);
console.log(legacy.shiftKey); // true

const modern = new MouseEvent("click", { shiftKey: true });
console.log(modern.shiftKey); // true
Try It Yourself

How It Works

  1. Legacy: 12th flag after coords is shiftKey.
  2. Modern: { shiftKey: true } in options.
  3. Both report shiftKey true.
  4. Named options are harder to mis-order.

Browser Support

MouseEvent.initMouseEvent() is marked Deprecated on MDN. Engines still implement it for createEvent compatibility. New code should use new MouseEvent().

✓ Deprecated

MouseEvent.initMouseEvent()

Legacy initializer for createEvent("MouseEvents"). Prefer MouseEvent() constructor. Still present for backward compatibility.

— Deprecated
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
initMouseEvent Legacy only

Bottom line: Understand it for old codebases; do not add it to new features. See the MDN initMouseEvent reference.

Frequently Asked Questions

It initializes a mouse event created with document.createEvent("MouseEvents"). You set type, bubble/cancelable flags, window, detail, coordinates, modifier keys, button, and relatedTarget before dispatchEvent.
Yes. MDN marks MouseEvent.initMouseEvent() as Deprecated. Do not use it in new code. Prefer the MouseEvent() constructor instead.
Use new MouseEvent(type, options) and dispatch with target.dispatchEvent(event). That is the modern Creating and dispatching events pattern on MDN.
document.createEvent is also obsolete. Some browsers still support createEvent("MouseEvents") plus initMouseEvent for compatibility, but both belong to the old DOM Events Level 2 workflow.
None. initMouseEvent returns undefined. It mutates the event object in place.
Only for some types such as mouseover and mouseout. For click, mousedown, mouseup, and similar, pass null.

Next: altKey

Learn the dedicated Alt / Option boolean on mouse events.

altKey tutorial →

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.

8 people found this page helpful