jQuery mouseover()

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

jQuery mouseover()

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

This event is generally used with mouseout() event.

Note: Most of the people are confused between mouseenter and mouseover.

Difference between mouseenter() and mouseover()

The mouseenter event is only triggered if the mouse pointer enters the selected element whereas the mouseover event triggers if the mouse cursor enters any child elements as well as the selected element.

Syntax:

  1. $(selector).mouseover()  

It triggers the mouseover event for selected elements.

  1. $(selector).mouseover(function)  

It adds a function to the mouseover event.

Parameters of jQuery mouseover() event

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

Example of jQuery mouseover() event

Let's take an example to demonstrate jQuery mouseover() 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 mouseover() event example 2

Let's see another example of jQuery mouseover() 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