dom / latest / mouseevent / mouseevent.html /

MouseEvent()

The MouseEvent() constructor creates a new MouseEvent.

Syntax

new MouseEvent(typeArg, mouseEventInit);

Values

typeArg

Is a DOMString representing the name of the event.

mouseEventInit Optional

Is a MouseEventInit dictionary, having the following fields:

  • "screenX", optional long, defaulting to 0, that is the horizontal position of the mouse event on the user's screen; setting this value doesn't move the mouse pointer.
  • "screenY", optional long, defaulting to 0, that is the vertical position of the mouse event on the user's screen; setting this value doesn't move the mouse pointer.
  • "clientX", optional long, defaulting to 0, that is the horizontal position of the mouse event on the client window of user's screen; setting this value doesn't move the mouse pointer.
  • "clientY", optional long, defaulting to 0, that is the vertical position of the mouse event on the client window of the user's screen; setting this value doesn't move the mouse pointer.
  • "ctrlKey", optional boolean value, defaulting to false, that indicates if the ctrl key was simultaneously pressed.
  • "shiftKey", optional boolean value, defaulting to false, that indicates if the shift key was simultaneously pressed.
  • "altKey", optional boolean value, defaulting to false, that indicates if the alt key was simultaneously pressed.
  • "metaKey", optional boolean value, defaulting to false, that indicates if the meta key was simultaneously pressed.
  • "button", optional short, defaulting to 0, that describes which button is pressed during events related to the press or release of a button:
    Value Meaning
    0 Main button pressed (usually the left button) or un-initialized
    1 Auxiliary button pressed (usually the middle button)
    2 Secondary button pressed (usually the right button)
  • "buttons", optional unsigned short, defaulting to 0, that describes which buttons are pressed when the event is launched:
    Bit-field value Meaning
    0 No button pressed
    1 Main button pressed (usually the left button)
    2 Secondary button pressed (usually the right button)
    4 Auxiliary button pressed (usually the middle button)
  • "relatedTarget", optional EventTarget, defaulting to null that is the element just left (in case of a mouseenter or mouseover) or is entering (in case of a mouseout or mouseleave).
  • "region", optional DOMString, defaulting to null, that is the ID of the hit region affected by the event. The absence of any affected hit region is represented with the null value.

In some implementations, passing anything other than a number for the screen and client fields will throw a TypeError.

Note: The MouseEventInit dictionary also accepts fields from UIEventInit and from EventInit dictionaries.

Specifications

Browser compatibility

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari WebView Android Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet
MouseEvent
26
12
11
No
15
7
4.4
26
14
14
7
1.5

Polyfill

You can polyfill the MouseEvent() constructor functionality in Internet Explorer 9 and higher with the following code:

(function (window) {
  try {
    new MouseEvent('test');
    return false; // No need to polyfill
  } catch (e) {
    // Need to polyfill - fall through
  }

    // Polyfills DOM4 MouseEvent
  var MouseEventPolyfill = function (eventType, params) {
    params = params || { bubbles: false, cancelable: false };
    var mouseEvent = document.createEvent('MouseEvent');
    mouseEvent.initMouseEvent(eventType,
      params.bubbles,
      params.cancelable,
      window,
      0,
      params.screenX || 0,
      params.screenY || 0,
      params.clientX || 0,
      params.clientY || 0,
      params.ctrlKey || false,
      params.altKey || false,
      params.shiftKey || false,
      params.metaKey || false,
      params.button || 0,
      params.relatedTarget || null
    );

    return mouseEvent;
  }

  MouseEventPolyfill.prototype = Event.prototype;

  window.MouseEvent = MouseEventPolyfill;
})(window);

See also

  • MouseEvent, the interface of the objects it constructs.

© 2005–2021 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/MouseEvent