jQuery mousedown()

The jQuery mousedown() method is used to attach a function to run when a mousedown event occurs i.e. when the left mouse button is pressed down, at the time while the mouse cursor is over the selected element.

Syntax:

To trigger the mousedown event for selected elements.

$(selector).mousedown()

To add a function to the mousedown event.

$(selector).mousedown(function)

Function:

  • It is an optional parameter.
  • The function parameter specifies the function to run when the event occurs.

Example1:

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#h1").mousedown(function(){
$( "div" ).text( "mouse down event triggered" ).show().fadeOut( 1000 );
});
});
</script>
</head>
<body>
<h1 id="h1">Left click on me.</h1>
<div></div>
</body>
</html>

Example2:

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
("p").mouseup(function(){
$("p").css("background-color", "yellow");
});
});
</script>
</head>
<body>
<p>Press down the mouse left button over me</p>
</body>
</html>