W3cubDocs

/jQuery

.mousedown()

.mousedown( handler )Returns: jQuery

Description: Bind an event handler to the "mousedown" JavaScript event, or trigger that event on an element.

This method is a shortcut for .on( "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:

<div id="target">
  Click here
</div>
<div id="other">
  Trigger the handler
</div>
Figure 1 - Illustration of the rendered HTML

The event handler can be bound to any <div>:

$( "#target" ).mousedown(function() {
  alert( "Handler for .mousedown() called." );
});

Now if we click on this element, the alert is displayed:

Handler for .mousedown() called.

We can also trigger the event when a different element is clicked:

$( "#other" ).click(function() {
  $( "#target" ).mousedown();
});

After this code executes, clicks on Trigger the handler will also alert 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. Not all browsers support this property (Internet Explorer uses button instead), but jQuery normalizes the property so that it is safe to use in any browser. The value of which will be 1 for the left button, 2 for the middle button, or 3 for the right button.

This event 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, drags away from it, and 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.

Additional Notes:

  • As the .mousedown() method is just a shorthand for .on( "mousedown", handler ), detaching is possible using .off( "mousedown" ).

Example:

Show texts when mouseup and mousedown event triggering.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>mousedown demo</title>
  <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
 
<p>Press mouse and release here.</p>
 
<script>
$( "p" )
  .mouseup(function() {
    $( this ).append( "<span style='color:#f00;'>Mouse up.</span>" );
  })
  .mousedown(function() {
    $( this ).append( "<span style='color:#00f;'>Mouse down.</span>" );
  });
</script>
 
</body>
</html>

Demo:

© The jQuery Foundation and other contributors
Licensed under the MIT License.
https://api.jquery.com/mousedown