Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   
   HTML   
   CSS   
   JAVA SCRIPT   
   JQUERY   




EVENTS - mousedown()


The mousedown() Event executes when a mouse button is clicked on an element(Be it a left click, right click or a middle button click).


Example :



<html>
  	<head>
    	<title> My First Programme </title>
  	</head>

  	<body>
    	<h1> JQuery </h1>
    	<p class = "para1"> First Paragraph </p>
    	
    	<img src = "/myImage.png" alt = "Bring the mouse pointer here"/>

      	<script src = "https://cdnjs.cloudflare.com/ajax/libs/JQUERY/3.3.1/jquery.min.js"> </script>
	  
      	<script>
      
			$('img').mousedown( function() { 
            	$('p').text("You have pressed the mouse button")
            });
            
      	</script>      
  	</body>
</html>  


Output :



So, if you look at the above code, we have a <p> element,


<p class = "para1"> First Paragraph </p>

And we have an image,


<img src = "/myImage.png" alt = "Bring the mouse pointer here"/>

Now, if we take a look at the JQuery statement,


$('img').mousedown( function() {
	$('p').text("You have pressed the mouse button")
});

At first we locate the image,


$('img')

Then we use the mousedown() event,


$('img').mousedown(...)

And put the function inside mousedown() event to change the contents of <p> element.


$('img').mousedown( function() {
	$('p').text("You have pressed the mouse button")
});

And the function,


function() {
	$('p').text("You have pressed the mouse button")
}

Changes the content of <p> with the text,


'You have pressed the mouse button'

When the mouse button(Be it a left click, right click or a middle button click) is clicked on the image.