jQuery mouseenter()

The jQuery mouseenter() method is used to attach a function to run when a mouseenter event occurs i.e. when the 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.

 

Example1:

<!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>

Example2:

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