jQuery mousedown()

Introduction

Press down the left mouse button over a <div> element to insert some text:

View in separate window

<!DOCTYPE html>
<html>
<head>
<script 
 src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js">
</script>/*ww w. j a  v a  2s .  com*/
<script>
$(document).ready(function(){
  $("div").mouseup(function(){
    $(this).after("<p style='color:green;'>Mouse button released.</p>");
  });
  $("div").mousedown(function(){
    $(this).after("<p style='color:purple;'>Mouse button pressed down.</p>");
  });
});
</script>
</head>
<body>

<div>Press down and release the mouse button over this div element.</div>

</body>
</html>

The mousedown event occurs when the left mouse button is pressed on the selected element.

The mousedown() method triggers the mousedown event.

The mousedown() method can also run a function when a mousedown event occurs.

Trigger the mousedown event for the selected elements:

$(selector).mousedown()

Attach a function to the mousedown event:

$(selector).mousedown(function)
Parameter OptionalDescription
function Optional. function to run when the mousedown event is triggered



PreviousNext

Related