If you don't need IE11 support (e.g. in tests), you can use the
MouseEvent constructor instead.
The attached Coffeescript helper will let you create mouse events:
$element = $('div') Trigger.mouseover($element) Trigger.mouseenter($element) Trigger.mousedown($element) Trigger.mouseup($element) Trigger.mouseout($element) Trigger.mouseleave($element) Trigger.click($element)
The dispatched events are real DOM events, which will trigger both native and jQuery handlers.
.trigger is simpler, but will only trigger event handlers that were bound by jQuery's
Real user actions trigger multiple events
Many user interactions trigger more than one event. E.g. when the user moves the mouse over a button and "clicks" the mouse, she actually triggers a sequence of events (
To help emulate this, the
Trigger helper has a couple of additional functions that emit event sequences:
Trigger.clickSequence($element) # triggers mouseover, mousedown, focus, mouseup, click Trigger.hoverSequence($element) # triggers mouseover, mouseenter Trigger.unhoverSequence($element) # triggers mouseout, mouseleave
You can also pass options, e.g. to simulate a right button click while the CTRL-key was pressed:
Trigger.click(button: 2, ctrlKey: true)
This was extracted from Unpoly.