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 click, dblclick, mouseup, mousedown.
MouseEvent derives from UIEvent, which in turn derives from Event. Though the MouseEvent.initMouseEvent() method is kept for backward compatibility, creating of a MouseEvent object should be done using the MouseEvent() constructor.
The type of device that generated the event (one of the MOZ_SOURCE_* constants). This lets you, for example, determine whether a mouse event was generated by an actual mouse or by a touch event (which might affect the degree of accuracy with which you interpret the coordinates associated with the event).
Initializes the value of a MouseEvent created. If the event has already been dispatched, this method does nothing.
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 EventTarget.dispatchEvent().
HTML
html
<p><label><inputtype="checkbox"id="checkbox"/> Checked</label></p><p><buttonid="button">Click me to send a MouseEvent to the checkbox</button></p>
JavaScript
js
functionsimulateClick(){// Get the element to send a click eventconst cb = document.getElementById("checkbox");// Create a synthetic click MouseEventlet evt =newMouseEvent("click",{bubbles:true,cancelable:true,view: window,});// Send the event to the checkbox element
cb.dispatchEvent(evt);}
document.getElementById("button").addEventListener("click", simulateClick);