docs.rodeo

MDN Web Docs mirror

MouseEvent

{{APIRef("UI Events")}} 

The MouseEvent interface represents events that occur due to the user interacting with a pointing device (such as a mouse). Common events using this interface include {{domxref("Element/click_event", "click")}} , {{domxref("Element/dblclick_event", "dblclick")}} , {{domxref("Element/mouseup_event", "mouseup")}} , {{domxref("Element/mousedown_event", "mousedown")}} .

MouseEvent derives from {{domxref("UIEvent")}} , which in turn derives from {{domxref("Event")}} . Though the {{domxref("MouseEvent.initMouseEvent()")}}  method is kept for backward compatibility, creating of a MouseEvent object should be done using the {{domxref("MouseEvent.MouseEvent", "MouseEvent()")}}  constructor.

Several more specific events are based on MouseEvent, including {{domxref("WheelEvent")}} , {{domxref("DragEvent")}} , and {{domxref("PointerEvent")}} .

{{InheritanceDiagram}} 

Constructor

Static properties

Instance properties

This interface also inherits properties of its parents, {{domxref("UIEvent")}}  and {{domxref("Event")}} .

Instance methods

This interface also inherits methods of its parents, {{domxref("UIEvent")}}  and {{domxref("Event")}} .

Example

This example demonstrates simulating a click (programmatically generating a click event) on a checkbox using DOM methods. Event state (canceled or not) is then determined with the return value of method {{domxref("EventTarget.dispatchEvent", "EventTarget.dispatchEvent()")}} .

HTML

<p>
  <label><input type="checkbox" id="checkbox" /> Checked</label>
</p>
<p>
  <button id="button">Click me to send a MouseEvent to the checkbox</button>
</p>

JavaScript

function simulateClick() {
  // Get the element to send a click event
  const cb = document.getElementById("checkbox");

  // Create a synthetic click MouseEvent
  let evt = new MouseEvent("click", {
    bubbles: true,
    cancelable: true,
    view: window,
  });

  // Send the event to the checkbox element
  cb.dispatchEvent(evt);
}
document.getElementById("button").addEventListener("click", simulateClick);

Result

{{EmbedLiveSample('Example')}} 

Specifications

{{Specifications}} 

Browser compatibility

{{Compat}} 

See also

In this article

View on MDN