jQuery mouseout()

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

jQuery mouseout()

The mouseout event is occurred when you remove your mouse cursor from the selected element .Once the mouseout event is occurred, it executes the mouseout() method or attach a function to run.

This event is generally used with mouseover () event.

Note: Most of the people are confused between mouseout and mouseleave.

Difference between mouseleave and mouseout

The mouseleave event is only triggered if the mouse pointer leaves the selected element whereas the mouseout event triggers if the mouse cursor leaves any child elements as well as the selected element.

Syntax:

  1. $(selector).mouseout()  

It triggers the mouseout event for selected elements.

  1. $(selector).mouseout(function)  

It adds a function to the mouseout event.

Parameters of jQuery mouseout() event

Parameter Description
Function It is an optional parameter. It executes itself when the mouseout event is triggered.

Example of jQuery mouseout() event

Let's take an example to demonstrate jQuery mouseout() event.

  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4. <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>  
  5. <script>  
  6. $(document).ready(function(){  
  7.     $("p").mouseover(function(){  
  8.         $("p").css("background-color", "lightgreen");  
  9.     });  
  10.     $("p").mouseout(function(){  
  11.         $("p").css("background-color", "orange");  
  12.       });  
  13. });  
  14. </script>  
  15. </head>  
  16. <body>  
  17. <p>Move your cursor over this paragraph.</p>  
  18. </body>  
  19. </html>  

Test it Now

Output:

Move your cursor over this paragraph.

jQuery mouseout() event example 2

Let's see another example of jQuery mouseout() event.

  1. <!DOCTYPE html>  
  2. <html lang="en">  
  3. <head>  
  4.   <meta charset="utf-8">  
  5.   <title>mouseover demo</title>  
  6.   <style>  
  7.   div.out {  
  8.     width: 40%;  
  9.     height: 120px;  
  10.     margin: 0 15px;  
  11.     background-color: lightgreen;  
  12.   }  
  13.   div.in {  
  14.     width: 60%;  
  15.     height: 60%;  
  16.     background-color: red;  
  17.     margin: 10px auto;  
  18.   }  
  19.   </style>  
  20.   <script src="https://code.jquery.com/jquery-1.10.2.js"></script>  
  21. </head>  
  22. <body>  
  23.  <div class="out">  
  24.   <span style="padding:20px">move your mouse</span>  
  25.   <div class="in"></div>  
  26. </div>  
  27. <script>  
  28. $( "div.out" )  
  29.   .mouseover(function() {  
  30.     $( this ).find( "span" ).text( "mouse over " );  
  31.   })  
  32.   .mouseout(function() {  
  33.     $( this ).find( "span" ).text( "mouse out " );  
  34.   });  
  35. </script>  
  36. </body>  
  37. </html>  

Test it Now