jQuery mouseenter()

The jQuery mouseenter() method is used to attach a function to run when a mouseenter event occurs i.e, when mouse cursor is entered over the selected element.

Syntax:

To trigger the mouseenter event for selected elements.

$(selector).mouseenter()

To add a function to the mouseenter event.

$(selector).mouseenter(function)  

Function:

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

 

Example 1

<!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").mouseenter(function(){    
   	$( "div" ).text( "Mouse is on the heading" ).show().fadeOut( 1000 );   
	});    
});    
</script>    
</head>    
<body>    
<h1 id="h1">Enter this heading.</h1>   
<div></div>   
</body>    
</html>

Try it

JS Bin on jsbin.com

Example 2

<!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").mouseenter(function(){  
    	$("p").css("background-color", "red");  
	});  
	$("p").mouseleave(function(){  
    	$("p").css("background-color", "yellow");  
	});  
});  
</script>  
</head>  
<body>  
<p>Move your mouse cursor over this statement.</p>  
</body>  
</html>

Try it

JS Bin on jsbin.com

Please Share