Mouse Events

These events are triggered by mouse movement and button presses.

Binds an event handler to the mousedown JavaScript event, or triggers that event on an element.

.mousedown(handler)
.mousedown()

This handler is a shortcut for .bind('mousedown', handler) in the first variation, and .trigger('mousedown') in the second.

The mousedown event is sent to an element when the mouse pointer is over the element, and the mouse button is pressed. Any HTML element can receive this event.

For example, consider the HTML:

The event handler can be bound to the target button:

Now if we click on the target button, the message is displayed. We can also trigger the event when the second button is clicked:

After this code executes, clicks on the trigger button will also display the message.

The mousedown event is sent when any mouse button is clicked. To act only on specific buttons, we can use the event object's which property in Mozilla browsers (1 for left button, 2 for middle button, 3 for right button), or the button property in Internet Explorer (1 for left button, 4 for middle button, 2 for right button). This is primarily useful for ensuring that the primary button was used to begin a drag operation; if ignored, strange results can occur when the user attempts to use a context menu. While the middle and right buttons can be detected with these properties, this is not reliable. In Opera and Safari, for example, right mouse button clicks are not detectable by default.

If the user clicks on an element, then drags the mouse pointer away from it or releases the button, this is still counted as a mousedown event. This sequence of actions is treated as a canceling of the button press in most user interfaces, so it is usually better to use the click event unless we know that the mousedown event is preferable for a particular situation.

Binds an event handler to the mouseup JavaScript event, or triggers that event on an element.

.mouseup(handler)
.mouseup()

Binds an event handler to the click JavaScript event, or triggers that event on an element.

.click(handler)
.click()

Binds an event handler to the dblclick JavaScript event, or triggers that event on an element.

.dblclick(handler)
.dblclick()

This handler is a shortcut for .bind('dblclick', handler) in the first variation, and .trigger('dblclick') in the second.

The dblclick event is sent to an element when the element is double‑clicked. Any HTML element can receive this event.

For example, consider the HTML:

The event handler can be bound to the target button:

Now if we double-click on the target button, the message is displayed. We can also trigger the event when the second button is clicked:

After this code executes, clicking the Trigger button will also display the message.

The dblclick event is only triggered after this exact series of events:

It is inadvisable to bind handlers to both the click and dblclick events for the same element. The sequence of events triggered varies from browser to browser, with some receiving two click events and others only one. If an interface that reacts differently to single and double clicks cannot be avoided, then the dblclick event should be simulated within the click handler. We can achieve this by saving a timestamp in the handler, and then comparing the current time to the saved timestamp on subsequent clicks. If the difference is small enough, we can treat the click as a double-click.

Binds two event handlers to the matched elements, to be executed on alternate clicks.

.toggle(handlerEven, handlerOdd)

Binds an event handler to the mouseover JavaScript event, or triggers that event on an element.

.mouseover(handler)
.mouseover()

Bind an event handler to the mouseout JavaScript event, or trigger that event on an element.

.mouseout(handler)
.mouseout()

Binds two event handlers to the matched elements, to be executed when the mouse pointer enters and leaves the elements.

.hover(handlerIn, handlerOut)

Binds an event handler to the mousemove JavaScript event, or triggers that event on an element.

.mousemove(handler)
.mousemove()

This handler is a shortcut for .bind('mousemove', handler) in the first variation, and .trigger('mousemove') in the second.

The mousemove event is sent to an element when the mouse pointer moves inside the element. Any HTML element can receive this event.

For example, consider the HTML:

The event handler can be bound to the target button:

Now when the mouse pointer moves within the target button, the message is displayed. We can also trigger the event when the second button is clicked:

After this code executes, clicking the Trigger button will also display the message.

When tracking the mouse movement, we usually clearly need to know the actual position of the mouse pointer. The event object that is passed to the handler contains some information about the mouse coordinates. Properties such as .clientX, .offsetX, and .pageX are available, but support for them differs between browsers. Fortunately, jQuery normalizes the .pageX and .pageY attributes so that they can be used in all browsers. These attributes provide the X and Y coordinates of the mouse pointer relative to the top‑left corner of the page.

We need to remember that the mousemove event is triggered whenever the mouse pointer moves, even for a pixel. This means that hundreds of events can be generated over a very small amount of time. If the handler has to do any significant processing, or if multiple handlers for the event exist, this can be a serious performance drain on the browser. It is important, therefore, to optimize mousemove handlers as much as possible, and to unbind them as soon as they are no longer needed.

A common pattern is to bind the mousemove handler from within a mousedown hander, and to unbind it from a corresponding mouseup handler. If implementing this sequence of events, remember that the mouseup event might be sent to a different HTML element than the mousemove event was. To account for this, the mouseup handler should typically be bound to an element high up in the DOM tree, such as <body>.