The mouseout() Event executes when a mouse pointer leaves an element as well as its child element.
<html>
<head>
<title> My First Programme </title>
</head>
<body>
<h1> JQuery </h1>
<p class = "para1"> First Paragraph </p>
<img src = "/myImage.png" alt = "Bring the mouse pointer here"/>
<script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"> </script>
<script>
$('img').mouseout( function() {
$('p').text("Mouse pointer left the image")
});
</script>
</body>
</html>
So, if you look at the above code, we have a <p> element,
<p class = "para1"> First Paragraph </p>
And we have an image,
<img src = "/myImage.png" alt = "Bring the mouse pointer here"/>
Now, if we take a look at the JQuery statement,
$('img').mouseout( function() {
$('p').text("Mouse pointer left the image")
});At first we locate the image,
$('img')Then we use the mouseout() event,
$('img').mouseout(...)And put the function inside mouseout() event to change the contents of <p> element.
$('img').mouseout( function() {
$('p').text("Mouse pointer left the image")
});And the function,
function() {
$('p').text("Mouse pointer left the image")
}Changes the content of <p> with the text,
'Mouse pointer left the image'
When the mouse pointer leaves the Image.
Now, let us take another example, where the <img> element is under a <div> element.
<html>
<head>
<title> My First Programme </title>
</head>
<body>
<h1> JQuery </h1>
<p class = "para1"> First Paragraph </p>
<div>
<img src = "/myImage.png" alt = "Bring the mouse pointer here"/>
</div>
<script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"> </script>
<script>
$('div').mouseout( function() {
$('p').text("Mouse pointer left the image")
});
</script>
</body>
</html>
In this case, we have the <img> element inside a <div> element.
<div> <img src = "/myImage.png" alt = "Bring the mouse pointer here"/> </div>
Now, if you see the JQuery statement,
$('div').mouseout( function() {
$('p').text("Mouse pointer left the image")
});We are calling the mouseout() event on the <div> element. And since the <img> element is inside the <div> element, the mouseout() event gets triggered.