19 Feb jQuery hover() method
Definition
Use the jQuery hover() method, to hover over a matched element and call two functions, one for mouseenter and another for mouseleave.
Syntax
The following is the syntax,
$(selector).hover(enterFunction,leaveFunction)
The following are the parameters,
- enterFunction: Function to run when the mouseenter event occurs. This is a required parameter.
- leaveFunction: Function to run when the mouseleave event occurs. This is an optional parameter.
Example
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
$("h2").hover(function(){
$(this).css("color", "blue");
});
});
</script>
</head>
<body>
<h2>Hover over me to change my color.</h2>
<p>Understanding the usage of hover() method in JavaScript.</p>
</body>
</html>
The following is the output,

Hover over the heading to change its color. The color will change to blue,

No Comments